The macro F() and the reserved word PROGMEM cause information to be written to the Arduino's flash memory.
What I do not understand is if a program with one or both of those constructs is used in setup(), and the Arduino is powered down, then powered up and run again, etc., does the flash memory get used up or does it somehow purge itself?
If F() and / or PROGMEM are used in the loop() function wouldn't that eat through the flash memory quickly?
In my searching I have not come across anything that mentions how the flash memory is recovered or how it is purged.
Thanks guix. If what you said is in any of the documentation, I have missed it.
One more question. If I use
Serial.print(F(“Hello World”));
to write that string to flash memory that one time, how do I access it at run time? With PROGMEM there is a clear variable associated with the data, but no obviously so with the Serial.print function.
If I use Serial.print(F("Hello World")); to write that string to flash memory
That doesn't "write the string" to flash memory; it causes the string to be created in flash memory (at compile time), and then prints it. Exactly the same as if it were Serial.print("Hello World"); would create the string in RAM and print it - you create a string in memory, but it has no symbolic name associated with it that would make it a "variable."
On some microcontrollers (notably ARM), using a literal string like "Hello World" would automatically cause the string to be stored in flash memory, but this isn't possible on AVR because flash is a separate "address space" and you need special code to access strings stored there.
The contents of SRAM is lost when the AVR looses power. So when the processor is restarted it requires that any constants held in SRAM are restored. C/C++ has a default value for all global var's. In addition all global's and all static var which don't use the default need to be setup.
Code is generated for you in order to do this.
Constants such as "This is a STring" are normally created in SRAM (for speed) by copying from EPROM when the processor restarts.
The F() macro tells the compiler to keep the string in eprom and not to create it in SRAM.