How can a string be stored in SRAM even though it is volatile?

Hello,

I have a small problem of understanding:
As I understand it, my Arduino has two or three memories: RAM, Flash and Eeprom.

  • The data in RAM, in the case of Arduino a SRAM, are volatile, so they are lost at every reset or power on and off.
  • The flash contains the actual code that is executed and this remains even after a restart.

Now I have looked at the F-Macro. This macro causes that strings, which for example are output via serial, are not stored in the SRAM, but in the flash memory.
My understanding problem is now the following: How can a string be stored in SRAM if it does not survive a restart?

I would be very pleased about answers!

Greetings
Marek Küthe

If you do not use the F macro, the strings are in program memory anyway, but are put into SRAM during startup, during the (hidden) initialization program phase.

3 Likes

As part of the startup, the content of those strings gets copied from Flash to SRAM - in the same way as any other initialised variables.

So, by putting your strings in Flash, you don't use up any more flash - you just save having to copy them into RAM

1 Like

Not quite.

The SRAM contents would actually survive just a reset - so long as the power remains OK.

But, as part of the (hidden) initialisation phase, the default is to clear the SRAM - which is how the data gets "lost".

(there are ways & means around this for times where you do want stuff in RAM to survive a reset-without-power-loss)

1 Like

Fun bit of trivia: An LED can generate enough power to keep the AVR SRAM stable.

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.