Serial.print(F(“Hello World”));

I found this comment on Serial.print:

You can pass flash-memory based strings to Serial.print() by wrapping them with F(). For example :

Serial.print(F(“Hello World”))

What does it mean to pass flash-memory based strings?

It means the string isn't using up RAM.

2 Likes

When you compile your program it says how much program memory (stored in flash) you are using and how much dynamic ram you are using.

If you use F() you can move constant strings to the program memory instead of the ram. This will take up space that will decrease the amount of other code you can write. But it will free up dynamic ram.

My chip has 32KB of Flash memory (2Kb taken up by bootloader) and 2KB or RAM. Sometimes I need to store lots of data in RAM so I move the constant strings to Flash memory. Sometimes my program has a lot of steps and I have a shortage of Flash memory so I leave the strings in RAM.

2 Likes

If you use F() you can move constant strings to the program memory instead of the ram.

Actually what happens in a Harvard architecture uC is that the compiled string stays in flash and does not get copied to SRAM during the C++ initialization that happens before your sketch receives run control.

Since the string is not moved to SRAM, it has the PROGMEM property and runs from flash.

Ray

5 minute read about flash and SRAM here

1 Like

CaverAdam:
Sometimes my program has a lot of steps and I have a shortage of Flash memory so I leave the strings in RAM.

That's not how it works.

Your string constants are ALWAYS in flash memory. They have to be, because when you take power away from RAM everything's gone. When you put power back on again, they have to be reloaded from somewhere, and that somewhere is the flash memory. Leaving off PROGMEM will do nothing to save you significant amounts of flash memory.

It's just that a normal part of initializing a C++ program is to load those arrays into SRAM before setup(). PROGMEM is an AVR-specific attribute that can be applied to variables that tells the compiler to not do that. Because flash is in a different memory space than RAM, special functions are needed to access from flash than from RAM.

2 Likes

Thanks for the correction.

1 Like

Thanks ALL !!!

So, how does it work with arduino DUE that have no accessible flash memory?

Serial.println(F("Super basic example... manage and track a click counter"));

Is it just ignore F() function ?
It still compile when calling it but if you have not add PROGMEM to the string, compiler will put it anyway in RAM. If I understand correctly...

how does it work with arduino DUE that have no accessible flash memory?

Where do you get the idea that Due does not have "accessible" flash memory? On an ARM chip, the flash memory is much more EASILY accessible than on an AVR. "const" and literal strings will normally be put in flash memory by default.

This original question wasn't answered. I found the answer here:

1 Like