2

Could someone give me a hint regarding what would be the best practice to save memory: should I #define strings or use local static const char arrays?

Code example, option #1:

#define         LCD_LIMIT   "Number of hours"
...
setup(){
    DisplayData(LCD_LIMIT, actual_Data);
}
...
void DisplayData(const char *theme, String data){
}

Code example, option #2:

setup(){
    DisplayData(actual_Data);
}
...
void DisplayData(String data){
    static const char theme[] = "Number of hours";
}

Cheers

  • 1
    Check out PROGMEM to put data in flash mem. – Johnny Mopp May 3 '17 at 13:33
  • Thanks @JohnnyMopp! Does it mean both my two options are similar in terms of memory footprints and digging PROGMEM is much more interesting? – Manitoba May 3 '17 at 13:55
4

The #define will be substituted in at compile time. So as far as memory usage goes

#define MYSTRING "hello world"
DisplayData(MYSTRING);

and

DisplayData("hello world");

are completely identical.

but

static const char theme[] = "hello world";
DisplayData(theme);

Will store the text in memory and then call DisplayData with a pointer to that memory location. This will require one memory pointers worth of memory extra (unless that is the compiler is being smart that day and realizes it can substitute in the string and save a few bytes).

However if you are using the same string twice or more then things are different.

The static const will only include the string once and then point to it each time, each reference to the string adds only a couple of bytes to the code size. If you #define the string then every time it is referenced the whole string is inserted into the code at a cost of n+1 bytes where n is the string length.

  • Thanks for this detailed reply. So does this also apply to static const char arrays declared only locally (and therefore only called when the function itself is called? – Manitoba May 3 '17 at 15:49
  • A static variable declared within a function is basically a global variable that the compiler won't let other parts of the code access. It exists at all times, not just when the function is called and so takes a constant amount of memory. If it was local but not static then it would only use RAM when in the function but you would have the performance hit of copying the string from flash to RAM every time the function was called. – Andrew May 4 '17 at 8:03
  • As Michel indicated in his answer the F() macro will store the string in flash and not copy it to RAM which will use less memory but may give a tiny performance hit. – Andrew May 4 '17 at 8:06
  • Thanks @Andrew for the clear explanation, I really got it. – Manitoba May 5 '17 at 7:14
1

I think they both are similar with respect to memory stored in the heap.

However, since you pass one extra pointer, it cost 2 of 4 bytes extra on the stack (temporary space).

To save more memory use the F() function to store it in FLASH (32 KB on Arduino Uno) instead of the heap (only 2 KB). By using

static const char theme[] = F("Number of hours");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.