We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I have several constant strings that I defined as follows:
const u8 *Stringname = "StringContent";
At some stage if I add one more string, the uC stops working. I have more than enough code memory left so it is not a memory problem.
What could be the problem? Am I doing this the wrong way? Should I put the strings in a different memory space?
Note that the former declaration causes the strings themselves to be placed into code memory, and then copied into RAM. The initializers have to be stored in some non-volatile fashion; hence their appearance in the code memory. The code that does this copying is in INIT.A51.
Dan's recommended declaration puts the strings in code memory and leaves them there. The compiler will have to generate MOVC instructions to fetch the data. This can be a little less efficient than MOVX access, since the 8051 instruction set is not quite as rich in MOVC addressing modes as MOVX. But it's close, and leaving constants in code space saves a lot of memory. (I also prefer to declare string constants this way. In fact, my projects don't include INIT.A51 at all, nor any static initializers on globals.)
At least in principle,
u8 *Stringname = "StringContent";
requires the compiler to allocate memory to store the string itself, plus more memory to store the pointer to the string.
On the other hand,
u8 String[] = "StringContent";
only requires storage for the text.
The question is: for a pointer to a const string, is the compiler smart enough to do away with the separate pointer...?