Storage of const string arrays

05 Apr 2011

I have noticed that even if an array of C++ strings is declared as const, the resulting data is stored in RAM rather than FLASH memory. For instance

const string teststring[] = {
     "String1",
     "String2",
};

stores the data in RAM. With large arrays - like text menus - this can eat up a lot precious RAM.

To confirm this, I made a test program that prints the amount of free memory either with or without an 8000-char const string array. Including the array causes free RAM to drop from 24,196 bytes to 13,476 bytes.

Import programstringtest

This program explores how the compiler treats const string arrays.

Am I missing a storage type keyword like "ROM" or "FLASH" or something?

05 Apr 2011

Don't use std::string here - it's a class (admittedly convenient) which does many things besides just keeping the characters. Try this instead:

const char * const mystrings[] = {
     "String1",
     "String2",
};
05 Apr 2011

Thanks, Igor. That did the trick.