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.
Hi,
is it true that #define directives are located on the stack (or in the flash)?
#define TEST "ein text"
Or is it much better to use
static unsigned char[] = "ein text";
variables?
many thanks for your answers
Peter
All these defines include all mimetypes from the http protocol. If a new http "packet" will arrive, I compare the header part with these defines.
#define HTTP_MIME_TYP_RANGE "Range:"
memcmp((void *) *pdata, (void *) HTTP_MIME_TYP_RANGE, strlen(HTTP_MIME_TYP_RANGE_BYTES))
thanks for your answers
Do you understand what the preprocessor actually does with #defines?
Whe you write
It means that, every single occurrence of the token HTTP_MIME_TYP_RANGE will be replaced with the text "Range:" (including the double quotes).
Clearly, if you use that token a lot, you could end up with a lot of identical strings filling up your meory space!
Therefore, it might save memory to have a single string constant, and refer to it wherever required.
Note that some compilers are smart enough to spot duplicate strings like this, and automatically combine them...
OK, there's a couple of exceptions; eg, within strings & comments.
I suggest that you get down you 'C' text book and study the chapter on the preprocessor for full details...
As Andy said, it's a question of saving memory (ROM, to be more specific.) If you don't want to rely on the compiler being smart enough to combine duplicate strings, you should declare the strings as variables. But you probably don't want to do it like you initially suggested:
This would waste RAM, and the unsigned is unnecessary. Instead, you should do it like this:
static const char mystring[] = "ein text";
Perhaps unrelated to the topic, but you also probably shouldn't use strlen(). This computes the length of the string at run time, which is unnecessary since it is known at compile time. Instead, use sizeof:
sizeof(mystring) - 1 /* don't forget about the terminating zero */
And yes, you should read a book on C.