cancel
Showing results for 
Search instead for 
Did you mean: 

Where does my const array go? (memory type?)

Chris Rice
Associate III

So, if we have a large amount of hard coded data we want to incorporate into my program, my presumption was that a const array would allow the compiler to treat the data more like code, than data, in terms of cost against your CPU limits.

This was almost certainly a simplification, but now I'm afraid it's a misunderstanding. When I define the three file-scoped arrays below, and compile, I get the memory reports below.  

I thought that adding the const would move the storage from 'ZI' to 'RO'. But adding const seems to have deducted it from ZI, but not added it to RO. Where does the const array go?  

Also I'm questioning my my assumption that Code+RO counts against code space, and RW+ZI counts against RAM. What is the proper math to be doing when making sure you have enough of both kinds of space?

Thanks!

uint16_t m_Data[50];

Program Size: Code=19776 RO-data=428 RW-data=196 ZI-data=38348  

uint16_t const m_Data[50];

Program Size: Code=19776 RO-data=428 RW-data=196 ZI-data=38244  

uint16_t const m_Data[50] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10};

Program Size: Code=19776 RO-data=428 RW-data=196 ZI-data=38244  

5 REPLIES 5

Check symbols in .MAP file, or printf() a pointer

ie printf("%p\n", m_Data);

Ideally the array will be left in FLASH

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Ozone
Lead

> Where does the const array go?

You can look it up in the map file.

Creating a (human readable) map file is an option of the linker stage, check with your IDE/toolchain.

But usually, constant data end up in Flash.

RO supposedly means Read Only data, ZI are Zero initialized data (done at startup, before calling main).

berendi
Principal

Does the program actually use the contents of the array? Or just pretend to use it? Or doesn't use it at all?

Compilers and linkers are pretty good at optimizing away data and code that can be computed runtime. E.g. when the compiler encounters

printf("%d\n", m_Data[3]);

if the contents are const, it can be reduced to

printf("%d\n", 0);

or

printf("%d\n", 4);

then the linker can drop the whole array, because it isn't referenced at all.

Bob S
Principal

> I thought that adding the const would move the storage from 'ZI' to 'RO'. But adding

> const seems to have deducted it from ZI, but not added it to RO. Where does the

> const array go? 

Assuming that, as @berendi​ said, that you reference these arrays in your code so that the optimizer doesn't throw them out - when you declare AND INITIALIZE an array as a normal "int array[50] = { 1, 2, etc. }", then there is a copy of the array in FLASH (as part of the "initialized data" section), and space reserved in RAM for the array. The startup code copies the data from FLASH into RAM to initialize the RAM copy.

When you declare the array "const", the copy in FLASH remains, and is "the" copy of the array that your program uses. There is no longer space reserved in RAM for the array (as is desired). That is why your FLASH (OK, "RO") size doesn't change when you change from "int array[]" to "const int array[]".

Chris Rice
Associate III

Thanks everyone, very helpful. I see that my experiment was bad, and my takeaway is that this should be in FLASH, but need to check the map file for sure.