do constant strings use RAM?

I've seen it many times before, where programs use a program memory alternative to Serial.println("text here"); in applications where more RAM is needed. why is it that the compiler makes that use RAM, though? and how exactly does it use RAM?

for example, if the program has many "quoted" (not variable) strings in it, do these all get stored in RAM is does it load a string into RAM when println is called and then unloads it afterwards, or does it keep the last string loaded until the next string related function is called? I'm guessing the first one, but I want to be a bit sure, since I've seen examples that actually copy it from PROGMEM to a global array with a set size, which would just make things worse.

thanks!

why is it that the compiler makes that use RAM, though?

The characters have to be stored somewhere. By default that is in SRAM.

for example, if the program has many "quoted" (not variable) strings in it, do these all get stored in RAM is does it load a string into RAM when println is called

No. The strings are stored in SRAM when the sketch starts.

and then unloads it afterwards

No. The string stay in SRAM.

but I want to be a bit sure, since I've seen examples that actually copy it from PROGMEM to a global array with a set size, which would just make things worse.

Not constant strings in print statements.

for every single string in the program? I wonder how my program hasn't run out of memory yet, I have stings all over the place, a few arrays and the SD card library in use, and yet it still runs fine.

if I try to use the F("stringHere") from the sticky in a function, I get this problem:

"cannot convert 'const __FlashStringHelper*' to 'char*' for argument '2' to 'void myFunction..."

so what would be the most effective and easy way to get all my functions to use PROGMEM strings instead?

I'm going to do a quick memory test to see if this is really what happens.
EDIT: sadly, it's true, no matter where the string is. surely the compiler should be able to automatically optimize this or something...

for every single string in the program?

For every unique string, yes.

If you have Serial.print("I got here"); 27 places in your code, there will only be one copy of the string kept.

I wonder how my program hasn't run out of memory yet, I have stings all over the place

Well, stings don't take up as much space as strings.

if I try to use the F("stringHere") from the sticky in a function, I get this problem:

You need to overload your function, as the Print class has, to support the F() macro.

so what would be the most effective and easy way to get all my functions to use PROGMEM strings instead?

There was a clue above.

1 Like

PaulS:

if I try to use the F("stringHere") from the sticky in a function, I get this problem:

You need to overload your function, as the Print class has, to support the F() macro.

is F the same as PSTR? I can only find information on that function on Google.

thatdude624:
is F the same as PSTR? I can only find information on that function on Google.

It is from WString.h:

class __FlashStringHelper;
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))

Alright, in the end I decided to use this:

#define P(str) (strcpy_P(bufferA, PSTR(str)), bufferA)

where bufferA is

char bufferA[32];

and was already in use in my program as a general purpose text holder that holds data for short/very short periods of time (for example, as an alternative to returning a delay). It works, thanks!

except //comments everything takes space...