Allocating strings to RAM?
(Feel free to delete this, as it is probably not in the right forum section. Sorry.)
This may be more of a C programming issue than one requiring a better understanding of embedded compilers. I'm just an old assembly language guy from 30 years ago and there's lots I don't know.
I'm trying to get my string to be modifiable by placing it in RAM. However, it is first declared with a default set, which means it actually points to Flash if I try to run it from Flash. Executing it from RAM works as expected. Flash copy to RAM creates a fault and I've haven't bothered to check into why, yet.
What would be considered best practice in a situation like this?
Is there a __attribute__ keyword that works for this? I've spent some time looking through the CrossWorks references for something that would help, but I may be overlooking something.
char *PrefsLimits[] = {"25-100", "0-120", "1-15", "1-15", "color", "color", "color", "color", "color", "color", "0-100", "~"};
char *PrefsUser[] = {"100", "30", "4", "2", "Blue", "Green", "Red", "White", "Blue", "Red", "1", "~"};
The above *PrefsUser contains the strings I wish to modify.
kind regards,
MAJ
-
#define MAX_STRINGS 12
#define MAX_STRING_SIZE 10
char PrefsUser[MAX_STRINGS][MAX_STRING_SIZE + 1] = {"100", "30", "4", "2", "Blue", "Green", "Red", "White", "Blue", "Red", "1", "~"};
From your example I'm not sure if you need the strings or the pointers to the strings to be variable (i.e. in RAM). A higher level view of what you are trying to achieve would be useful in order to best advise. Maybe supply more code (even if it does not work/compile) along the lines of what you want to do.
Regards, Trevor
-
Thanks, Trevor.
Paul Curtis of Rowley already responded to my request. If I may, I'll quote his suggestion, as I've been using his tip with great success:
QUOTE:
Paul Curtis, Dec 31 11:02 am (GMT):
In C, quoted string literals may be placed in RAM or in read-only memory by the compiler. The above is allowed because originally K&R C did not have a "const" keyword and C89 and C99 feature backward compatibility.
How would I do this?
char Prefs0[25] = "100";
char Prefs1[25] = "30";
char Prefs2[25] = "4";This allows 25 characters per preference setting and initializes the preference.
Now, you need:
char *PrefsUser[] = { Prefs0, Prefs1, Prefs2...
Note that the array in PrefsLimits will be stored in RAM (as it is not const). This is hard for some people to grasp, and I've seen it time and again. As such, to reduce RAM usage, you might want:
const char * const PrefsLimits[] = ...
And
char * const PrefsUser[] = ...
Please sign in to leave a comment.
Comments
3 comments