Search code examples
c++armcc

Declaring a type (enum, struct etc.) in class definition increase code size


I am developing some embedded software with armcc compiler. For debugging purposes optimizations are kept at minimum wiht -O0 flag. In order to improve code clarity I have moved some enum and struct definitions into a class with public access.

From this:

enum A{
 a,
 b,
 c,
 d
};

struct C{
 int q;
 int w;
 int e;
};

class myClass{

....
};

To this:

class myClass{
 public:
    enum A{
     a,
     b,
     c,
     d
    };

    struct C{
     int q;
     int w;
     int e;
    };
....
};

And reaching them through myClass like;

myClass::C new_struct;
new_struct.q= myClass::a;

But to my surprise even with no other difference in code it increased the code size like 600 bytes. I assume that it is caused by compiler optimizations (even at -O0 there are some optimizations active) of replacing enumerations with direct values but not sure about this. What may have caused the increase in code size (or prevented optimization if it is the case)?


Solution

  • For both builds there are debug data embedded into elf.

    Debug symbol table included in the image contains information about each item defined in your program. When you move an enum and a struct inside a class, the names related to elements of the enum and the struct become larger: A::a becomes myClass::A::a, A::b becomes myClass::A::b, and so on. All these longer strings take additional space in the image file, making it larger.

    Stripping the debug symbols should make the two codes produce images of identical size.