Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 9 months ago.
Question about Memory
I would like a question about Memory on this device. I read that in this device the program >and< the constants are put on flash. While the variables use SRAM. This is different than in the arduino where if you want to put the constants on flash you have to use the keyword PROGMEM and use special functions to use them
So questions 1) do constants and array of constants are put automatically on flash? 2) to use these constants, can I use just normal pointers?
Any other thing I should be aware to efficiently manage my constants??
thanks
Question relating to:
1 Answer
8 years, 9 months ago.
Anything defined as const will normally automatically be placed in flash and there is no need to do anything special to use them.
The compiler is smart enough to realize that it doesn't need to make a copy in ram if the value doesn't change, it can just use the copy that's in flash. The ARM memory model is that there is only one address space, values within a certain range represent flash, a different range represents the ram. As far as the program that is running is concerned a memory address is a memory address, where the hardware goes to get the information is irrelevant.
The down side to this approach is that the flash is often slightly slower than the ram.
Thank you very much for your reply. I have very much accepted it as an answer but I would like to ask you the following (related) Say I have a constant array : const uint8_t SmallFont[1144] ={......}
and a function say:
void setFont(uint8_t* font);
when I try to do this: setFont(SmallFont);
I get an error about incompatible types.... so I did this: setFont(( uint8_t *)SmallFont); and it works.
My question is, if I am casting it as a non constant, am I doing something wrong in terms of memory?? (Copyint it into the RAM or something??)
Thanks
posted by 15 Feb 2016The best solution would be to change the function to be void setFont(const uint8_t* font);
There is no situation where setFont should modify the font data, by defining the function in that way would throw up compiler errors if it tried to.
However the ideal solution isn't always an option, the cast you are doing will be fine, it shouldn't cause the data to be copied to RAM.
posted by 15 Feb 2016