Pages: [1]   Go Down
Author Topic: do constant strings use RAM?  (Read 504 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
« Last Edit: February 13, 2013, 10:10:54 am by thatdude624 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Austin, TX
Online Online
Faraday Member
**
Karma: 71
Posts: 6127
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

It is from WString.h:
Code:
class __FlashStringHelper;
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pakistan
Offline Offline
Sr. Member
****
Karma: 6
Posts: 357
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

except //comments everything takes space...
Logged


Pages: [1]   Go Up
Jump to: