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.
9 years, 11 months ago.
Strings, RAM and flash storage
I have a lot of strings for outputting to serial. What is the best way to handle memory footprint? I could create a header with a bunch of const variables for each string. Which would be a pain (lazy). Is there anything like Arduino F() where you can use inline to store string in flash? Maybe I can port that over?
What are most people doing?
Thanks
1 Answer
9 years, 11 months ago.
You shouldn't need the Arduino/AVR hack on ARM processors. For ARM targets, const data will go into FLASH and not use any RAM. On AVR's the workaround is required since different CPU instructions are used to access FLASH memory in comparison to RAM. On ARM, the same instructions are used for both.
So that would mean making a const variable for every string correct? I could make a header file to store all of those. I can't do inline in say a printf statement?
Thanks.
posted by 31 Dec 2014The strings that you would inline in a printf() statement are const. How would you modify them? :)
Consider the following code:
#include "mbed.h" DigitalOut myled(LED1); int main() { printf("Hello world\r\n"); while(1) { myled = 1; wait(0.2); myled = 0; wait(0.2); } }
If we look at the disassembly we see that the printf() is converted to a puts() call since we aren't actually formatting anything and just outputting a constant string. Still valid for our investigation though. If we look at line 0x122 we see it place the address of the "Hello world\r\n" string into r0. The address it places in this register is 0x00002284, which is safely in the FLASH area on my LPC1768 where I ran this sample.
00000120 <main>: 120: b508 push {r3, lr} 122: 4808 ldr r0, [pc, #32] ; (144 <main+0x24>) 124: f001 fc54 bl 19d0 <puts> ... 144: 00002284 .word 0x00002284
We can dump the memory at 0x2284 and see our string in FLASH:
2280: 9E 46 70 47 48 65 6C 6C 6F 20 77 6F 72 6C 64 0D .FpGHello world. 2290: 00 00 00 00 63 6F 75 6C 64 20 6E 6F 74 20 70 69 ....could not pi
I hope that helps.
Thank you taking the time to break it down. It does help! I am so used to Arduino stuff. I was thinking I was using more RAM than before. I have more RAM\flash available and yet running out with mostly the same code as on the ATmega32u4. Hard to tell if the issue is out of flash or RAM. I switched from Arch to Arch Pro and now there is enough. But final design will probably be on one of the ST Nucleo boards.
posted by 31 Dec 2014