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
#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.
i want the smallest footprint as possible. I want to use C++ and strings
Those two statements contradict each other. You're just beginning to find out how badly.
In C++, you don't pay for what you don't use. There is no reason a C++ program will be (significantly) bigger or slower than a comparable C program; consider using "stdio.h" instead of "iostream.h".
Note that the string class in C++ is designed for use with dynamically allocated memory, to allow it to perform string concatenation.
So even if your program does strip away some of the unused string functionality, there can still be a significant amount of code that gets brought in. How much depends a lot on exactly how the string class is implemented.
You can manage quite well writing small C++ programs, but will always have to be prepared for the implications when you include standard C++ classes - they are designed as a framework, intended to be used in situations where every kB of code space isn't important but where the focus is on productivity.
So feel free to use C++ as a "better C" even when doing embedded development. But think twice about the use of STL etc. And think twice about using code that depends on generic dynamic memory allocations when using a small embedded processor - the issues with fragmented heap is way bigger when you need to limit yourself to a very small heap. On a PC, it's normally always possible for the runtime library to request more memory from the OS if it's impossible to fulfill an allocation request within the current heap size.
"You can manage quite well writing small C++ programs"
I should of course have written "witing space-efficient C++ programs"