Go Down

Topic: do constant strings use RAM? (Read 552 times) previous topic - next topic

thatdude624

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!

PaulS

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

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

Quote
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.

Quote
and then unloads it afterwards

No. The string stay in SRAM.

Quote
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.

thatdude624

#2
Feb 13, 2013, 04:01 pm Last Edit: Feb 13, 2013, 04:10 pm by thatdude624 Reason: 1
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...

PaulS

Quote
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.

Quote
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.

Quote
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.

Quote
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.

thatdude624


Quote
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.

James C4S


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


It is from WString.h:
Code: [Select]
class __FlashStringHelper;
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

thatdude624

Alright, in the end I decided to use this:
Code: [Select]
#define P(str) (strcpy_P(bufferA, PSTR(str)), bufferA)
where bufferA is
Code: [Select]
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!

Khalid

except //comments everything takes space...

Go Up