While saving ESP8266 RAM spaces I am confused by the usage of PSTR(), PROGMEM, and lots of _P functions.

What I know now is PROGMEM only applies to global or static strings, while PSTR() can only be used in functions.

The definition of PROGMEM and PSTR() is like:

#define PROGMEM __attribute__((section( "\".irom.text." __FILE__ "." __STRINGIZE(__LINE__) "."  __STRINGIZE(__COUNTER__) "\"")))
#define PSTRN(s,n) (__extension__({static const char __pstr__[] __attribute__((__aligned__(n))) __attribute__((section( "\".irom0.pstr." __FILE__ "." __STRINGIZE(__LINE__) "."  __STRINGIZE(__COUNTER__) "\", \"aSM\", @progbits, 1 #"))) = (s); &__pstr__[0];}))

...and I see one stores strings in .irom.text and the other in .irom0.pstr. Seems like PSTR() is the "extended version" of PROGMEM.

I've tested them like this:

static PGM_P progfmt PROGMEM = "I'm PROGstr. %s\n";
PGM_P pstrfmt = PSTR("I'm PSTRstr. %s\n");

Serial.printf_P(progfmt, pstrfmt);
Serial.printf_P(pstrfmt, progfmt);

They work alike..?

...and this panics.

static PGM_P prog_pstr PROGMEM = PSTR("I'm both. %s\n");

My questions are:

  • What is the difference between them (or technically, .irom.text and .irom0.pstr) ? Do they all save RAM by storing strings in Flash? And when to use which?
  • What about type PGM_P (const char *)?
  • Is there anything to consider while using them with _P functions? Any behavior differences?
  • The ESP8266 is a 32Bit CPU with a flat memory model. The answers given in arduino.stackexchange.com/questions/89159/… should apply 1:1. Bottom line: Neither of this is necessary.
    – PMF
    Apr 9 at 11:25
  • use F() to save RAM on ESPs.
    – dandavis
    Apr 10 at 9:30
  • @dandavis The use of F() has no impact on the use of RAM whatsoever on these CPUs.
    – PMF
    Apr 10 at 11:14
  • @PMF: I use it on web responses all the time. Compiling for nodemcu, I tested F() a 9693b string in one of my sketches with and without, with F(): ... leaving 24928 bytes..., w/o F(): ...leaving 15232 bytes.... Without it, i even get the ole Low memory available, stability problems may occur. It sure seems like it saved the RAM, how do you explain? Is the IDE and ESP.getFreeHeap() after boot wrong?
    – dandavis
    Apr 10 at 16:49
  • 1
    @PMF esp32 is different in this. on esp8266 PROGMEM is useful
    – Juraj
    May 20 at 17:18


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.