1

While restucturing a part of my code into a class I chose to change a static sized array into a dynamic array and was shocked about the size of my sketch, which increased by ~579%!

Now there is some discussion going on about wheather to use new or malloc() but I did not find a hint to this massive increase in sketch size.

  • So, if anybody would like to explain where this huge increase is comming from, that would be great!
  • Further, if anybody knows simmilar pitfalls, it would be very nice of you to share ;)

Here is a demo code to check for yourselfs:

void setup() {
  // put your setup code here, to run once:
  #define BUFLEN   8 * sizeof(char)
  #define BUFVAL   '5'
  #define BUFARR   {BUFVAL,BUFVAL,BUFVAL,BUFVAL,BUFVAL,BUFVAL,BUFVAL,BUFVAL,0}
  #define MODE     2
  int i = 0;
  Serial.begin(115200); 

  #if (MODE == 0)
    //10.772 Bytes for an Arduino Due on IDE 1.57 -> 2% of total
    char empty_page[BUFLEN+1] = BUFARR;
  #elif (MODE == 1)
    //12.772 Bytes for an Arduino Due on IDE 1.57 -> 2% of total, ~18.5% increase
    char *empty_page = (char *)malloc(BUFLEN+1);
    memset(empty_page, BUFVAL, BUFLEN);
    empty_page[BUFLEN+1] = '\0'; // NULL Terminate
  #elif (MODE == 2)
    //73.152 Bytes for an Arduino Due on IDE 1.57 -> 13% of total, ~579% increase 
    char *empty_page = new char[BUFLEN+1]BUFARR;
  #endif

  Serial.println("Result: ");
  for(i=0; i<BUFLEN; i++) {
    Serial.print(empty_page[i]);
  }
  Serial.println("");

  #if (MODE == 1)
    free(empty_page);
  #elif (MODE == 2)
    delete[] empty_page;
  #endif

}

void loop() {
  // put your main code here, to run repeatedly:

}

To check this code without arduino: http://ideone.com/e.js/bMVi0d


EDIT:

My understanding is, that new leads the IDE to compile in some large c++ stuff in order to handle it. On the other hand, the verbose compiler output of the IDE is identical.

I am trying to minimize my sketches, anybody else with this goal would sure be interested as well in parts like ´new´ that you need to avoid in order to get a smaller sketch. This seems to be a general Arduino IDE thing, so there should be a more meta explaination for it.

Jook
  • 4,564
  • 3
  • 26
  • 53
  • 2
    Make your linker generate a map file- you'll immediately see what's taking up all that space. – Carl Norum Feb 22 '15 at 04:32
  • @CarlNorum could you elaborate? the verbose output of the IDE is identical for MODE 0 and MODE 2. Using ´avr-size´ or ´avr-objdump´ as it is suggested to get to this map-file involves setting up cygwin on Windows, which is another box of worms and to be honest, I don't know how this will help. – Jook Feb 22 '15 at 05:28

2 Answers2

0

The new operator is essentially a type safe version of malloc meant to reduce errors in C++. You can see from the code here that new actually just calls malloc with a few bells and whistles added on. As far as when to use new vs malloc, one great discussion can be found here where the main verdict is that almost all C++ programs should use new. That being said, you do not need the extra bells and whistles for making a char array as you don't need to call a constructor for primitive types (calling a constructor is one of the main functions of the new operator. Also of note, primitive types do not have constructors at all so a compiler may lose performance searching for one). If memory is of dire concern, malloc is a perfectly acceptable solution. Your code with malloc looks perfectly fine and should work better for your purposes.

Community
  • 1
  • 1
David
  • 757
  • 1
  • 7
  • 17
  • in case of Arduino it seems to be way more than a few bells, it adds over 60kB to the binary sketch size - I would like to know why and why the malloc version is using so much less if it is basically the same. – Jook Feb 22 '15 at 06:14
0

You're confusing a few parts. The "IDE" is the Integrated Development Environment. That's a GUI which acts as an integrated front end for a few tools, including the compiler and linker. This problem looks like a linker problem.

In particular, this looks like a very poor compiler. It drags in about 60 kB while it should drag in nothing. Type safety should be handled by the compiler. Here the compiler should have told the compiler to just use malloc instead of new[], as all the type checks pass.

You should understand that the Arduino is a cheap product and the tooling isn't exactly state of the art.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • The Arduino IDE uses the gcc compiler and by the way the Arduino IDE is not cheaper than gcc (they are both gratis). While the Arduino IDE is not super advanced it generates the same binaries as anything else that uses gcc. – Rasmus Friis Kjeldsen Aug 29 '16 at 19:37
  • @rasmus: I said the Arduino itself is cheap. The consequences include a lack of financing for the Arduino backend in Gcc. – MSalters Aug 30 '16 at 07:21