Skip to main content
lowpowermcu
Associate III
July 14, 2012
Question

Best declaration of array of strings

  • July 14, 2012
  • 2 replies
  • 1408 views
Posted on July 14, 2012 at 17:09

Hi all,

I am developping an application on 16K RAM device and I am facing limitation of RAM size so I want to optimize the definition of the following pointer to strings and I want to  know if I am well understanding the stuffs

When I define the array as following, the whole buffer (128 x string size) will be programmed in RAM:

uint8_t* Buffer[128] = {

''STR1'', ''STR2'',.....};

When I define the array as folllowing, the whole buffe is programmed  in flash memory and hence I optimized the RAM memory

const uint8_t Buffer[128] [20]= {

''STR1'', ''STR2'',.....};

What do you think?

MCU Lüfter
    This topic has been closed for replies.

    2 replies

    Tesla DeLorean
    Guru
    July 14, 2012
    Posted on July 14, 2012 at 17:58

    static uint8_t const * const strtbl[] = {

    ''STR1'', ''STR2'', ''STR3'' };

    The less fixed dimension you give it, the more leeway the compiler has to implement it most efficiently.

    static may be overkill, but limits the scope to a single object and thus decisions about how it might be exported or utilized/exploited by others are removed. It also helps isolate ''where used'' questions.
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    flyer31
    Senior
    July 17, 2012
    Posted on July 17, 2012 at 07:09

    Did you try also

    const uint8_t* Buffer[128] = { ''STR1'', ''STR2'',.....};

    or

    uint8_t* const Buffer[128] = { ''STR1'', ''STR2'',.....};

    or

    const uint8_t* const Buffer[128] = { ''STR1'', ''STR2'',.....};

    (Please recognize, that it is a significant difference, whether you place the const after or before the ''*'' - one time the Buffer contents is const, one time the Buffer pointer).