Go Down

Topic: Why does the usage of 'new' increase the size of the program significantly? (Read 530 times) previous topic - next topic

DEiE

Disclaimer: I'm an intermediate programmer, all the low level C/C++ and microcontroller programming is new to me though. I know how pointers work, but I haven't applied them in this field.

I'm currently embarking on the mystical realm that is microcontroller programming, and I'm running into an anomaly that I can't explain.

I've created a simple program for treating an input pin as a toggle button, nothing fancy. It contains simple boolean logic, calls digitalRead, has two booleans and an unsigned char as members, and takes up three bytes of RAM, according to sizeof. I've put it on Github if you need to know more.

If I create a new instance of this class like
Code: [Select]
ToggleButton::ToggleButton toggleButton(BUTTON_PIN);
the program's total size (according to the compiler log) is 1218 bytes.

However, if I create the instance with new, like
Code: [Select]
ToggleButton::ToggleButton* toggleButton = new ToggleButton::ToggleButton(BUTTON_PIN);
the sketch suddenly becomes 1774 bytes.

I can't figure out where this increase in size comes from, or how I can avoid it, because 550 bytes is a pretty significant increase if you only have 32k, and I really want to use C++ and object oriented programming.

Can anyone enlighten me?

Erdin

I think the 'new' allocates memory, so the functions for memory allocation have to be included.
The Arduino doesn't include unused functions in the compiled code.
A function can use other functions, that is why for example using a single library function can enlarge the code significantly.

DEiE

If it is a one-time investment, then it is fine by me :). I'd like it if it didn't take 0.5kb, but if it only needs it once then it is not that bad. Thanks!

skyjumper

Yes its a one time investment, as you put it. This is true with any good programming environment, whether you're coding for Arduino, Linux, Windows, whatever. The linker should not include stuff that you didn't use. There are piles and piles of linker switches to configure and customize what's linked, how its linked and so on.

But the bottom line is, once its linked, its linked. You won't incur the same penalty for each call to new.


spatula


I can't figure out where this increase in size comes from, or how I can avoid it, because 550 bytes is a pretty significant increase if you only have 32k, and I really want to use C++ and object oriented programming.


Note that you may be doing object oriented programming even if you do not create class instances through the new operator. And the new operator is not a constructor, so there's nothing especially object-oriented in it. Part of the overhead you see is due to the compiler's having to compile/link the new operator itself.

Go Up