Jacky Yan
Jacky Yan

Reputation: 109

const char * array does not map into the flash

I know if we want to map a const string in the flash data, we can write:

static const char example[] = "map_in_flash";

We can find it in the flash memory at 0x08xxxxxx;

But I want to map a const string array in the flash and write:

static const char * example_array[10] = {"abc","def","ddd","fff"};

It is mapped in the ram.

                0x20018ae4       0x28 Src/main.o
 .data.example_array
                0x20018b0c       0x28 Src/main.o

How to map the const string array in the flash data?

Upvotes: 1

Views: 893

Answers (2)

Aleksi Torhamo
Aleksi Torhamo

Reputation: 6632

"When in doubt, add more const" - in other words, your array contains values of type "pointer to const char", which means the strings itself can't be changed, but the elements of the array can still be modified to point to different strings!

You'll want to use const char * const to make it so the pointers can't be changed either, and they can reside in the flash.

Upvotes: 3

KamilCuk
KamilCuk

Reputation: 141155

Non-const pointers are mutable. Make the pointers const.

static const char * const example_array[10] = {"abc","def","ddd","fff"};

If you use gcc and you really want to force your compiler to put the data inside read only part of memory, you could put them inside .rodata section with compiler specific section attribute specifier like __attribute__((__section__(".rodata"))).

Upvotes: 2

Related Questions