Hello;
I have a large const array that can and should be placed into flash memory. Is there any way to tell the gcc compiler to do this?
Hello;
I have a large const array that can and should be placed into flash memory. Is there any way to tell the gcc compiler to do this?
static const int mytable = {1,2,3,4};
ends up in the .text segment, ie flash.
Double check by diassembling the a.out file
The const keyword is the important one here.
While static const would work, the static implies the scope or portion of the program that knows about the definition of the declared variable. So…
const int mytable = {1,2,3,4}; // works for a table of integers
const char *keywords = {“IF”,“THEN”,“ELSE”}; // if you need a list of strings
I was going to comment on this early but decided against it. Now I think I need to.
It is my understanding (and experience) that the const keyword should not be relied on to put something into FLASH. What const does is inform the compiler that the flagged variable is not to be allowed to change. ONE way to do this is to put it into FLASH. Another way is to put it into a memory segment that is flagged as read only by a MMU. Other ways are for the compiler simply to keep its bookkeeping straight and not let you play with a const. So the keyword does not guarantee FLASH just read-only.
A case in point is the Arduino. Make something const and it does NOT go into FLASH. In fact it takes up precious RAM resources. You need special keywords to put the data into FLASH.
So the lesson here is that although the ARM compiler (at the very least IAR) may use const as a way to put variables into FLASH it is not a C language imperative that it does. In fact, think about a RAM only processor that still needs the const functionality.
fil-freak,fll-freak:
the OP was about the ARM gcc compiler, and my answer, too.
'night
E
Yes, this will be subject to the compiler, linker script and other variables.
For the ARM, gcc will place const arrays into Flash in most cases (I say most because there is probably some compile switch setting that would override it).
You can also declare variables with initializers without the const declaration, and in this case the initializer is normally part of the Flash space and is copied into RAM at startup.
fil-freak,
the OP was about the ARM gcc compiler, and my answer, too.
And that is why I was not going to comment! But my concern was that people browsing would think that const=FLASH and hence the post.