This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

#define located on stack / in flash

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

Parents
  • 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.

    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:

    static unsigned char[] = "ein text";
    


    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.

Reply
  • 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.

    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:

    static unsigned char[] = "ein text";
    


    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.

Children
No data