5

I believe PROGMEM is used to store large data (e.g. strings) that don't change. So it frees up the SRAM.

in the Ardunio Reference it is mentioned that.

"Using PROGMEM is also a two-step procedure. After getting the data into Flash memory, it requires special methods (functions), also defined in the pgmspace.h library, to read the data from program memory back into SRAM, so we can do something useful with it."

If I have to load the data into SRAM before using it then how is it efficient than directly stroing into SRAM by defining vaiable normally

10

Say you have a string of 1000 characters in Flash, and you want to print it to Serial. Yes, you need to load it into RAM in order to pass that data around the place for printing. But, do you need to put all of it in RAM at once? No. You only need one character at a time. So instead of taking 1000 characters and allocating 1000 bytes of RAM and copying the whole shebang into RAM then printing it, you just need to allocate 1 byte of RAM* and copy each character in turn and print it.

Since you can only read one character at a time from flash there's no advantage in reading the whole lot anyway.


*Actually, because of compiler optimizations, this probably never even touches RAM and just stays in a CPU register.

4

If you have a constant c-string (char array) in your code without F macro or progmem, then the string is copied into RAM at start an stays there.

If in a function you use the F macro or you create a char buffer of some size and copy the progmem string into the buffer, on function return the variables are deleted from stack and the memory is released.

In case of some big array, if you put it into progmem and then in the code when you need to lookup a value in that array, you need to copy to RAM only one array item at time not the whole array.

1

Like Majenko said, you do not need to put everything you have in flash storage into RAM at once. You can move items into SRAM as necessary, and reuse the memory once you're done.

However, you are not limited to moving one byte at a time. There are functions defined in avr/pgmspace.h that can handle moving data for you.

For instance, you could do something like:

const struct mystruct_s my_value PROGMEM = { value = 1234 };

// ...

struct mystruct_s working_copy;
memcpy_P(&working_copy, &my_value, sizeof(struct mystruct_s));
int foo = working_copy.value / 2;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.