This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Reduce size code using strings

Hi everybody.

I am developping an embedded application using an ARM cortex M0. I have 32k of program memory, so i want the smallest footprint as possible. I want to use C++ and strings because in the near future it's probable that I need some caracteristics.

There is any method to reduce the code size removing unused methods of the string class?

I have write the following code to test the size code:

MyClass1.h

#ifndef _MyClass1_h_
#define _MyClass1_h_

class MyClass1{
public:
   MyClass1();
   void Method1(void);
};

#endif

MyClass1.cpp

#include <string>
#include "MyClass1.h"

MyClass1::MyClass1(){
}

void MyClass1::Method1(void){
   std::string s = "test";
}

MyClass2.h

#ifndef _MyClass2_h_
#define _MyClass2_h_

class MyClass2{
public:
   MyClass2();
   void Method1(void);
};

#endif

MyClass2.cpp

#include "MyClass2.h"
#include "MyClass1.h"

MyClass2::MyClass2(){
}

void MyClass2::Method1(void){
   MyClass1 c1;
   c1.Method1();
}

MyClass3.h

#ifndef _MyClass3_h_
#define _MyClass3_h_

class MyClass3{
public:
   MyClass3();
   void Method1(void);
   void Method2(void);
};

#endif

MyClass3.cpp

#include "MyClass3.h"
#include "MyClass2.h"

MyClass3::MyClass3(){
}

void MyClass3::Method1(void){
   MyClass2 c2;
   c2.Method1();
}

void MyClass3::Method2(void){
   for (int i = 0; i < 100; i++);
}

Well, if I use the following Main I get a code size of 1556

Main.cpp

#include "MyClass3.h"

int main(void){
   MyClass3 c3;
   c3.Method2();

   while(1);
}

However, if I use the following Main I get a code size of 22956

Main.cpp

#include "MyClass3.h"

int main(void){
   MyClass3 c3;
   c3.Method1();

   while(1);
}

Viewing the results, I realize that the unused methods of my classes are removed and so, the code size reduced. Now, my question is if there is any way to remove the unused methods of the string class ?.

In the above example I only use the constructor of the string class, so I don't understand because the code size increases from 1k to 22k. I also check the cross-module optimization in the target options but I have exactly the same results.

I am waiting a response as soon as possible.
Thanks in advance.