const char s1[] PROGMEM = "I AM PREDATOR";
static const char s2[] PROGMEM = "UNSEEN COMBATANT";
am I correct that both strings will be in progmem but
s1 will be in stack memory
s2 will be in static memory ?
question came up after reading Issues with strstr_P (PROGMEM) - #4 by Coding_Badly
A variable marked PROGMEM cannot be a local variable.
(Flash memory can't be used as stack)
alexblade:
const char s1[] PROGMEM = "I AM PREDATOR";
static const char s2[] PROGMEM = "UNSEEN COMBATANT";
am I correct that both strings will be in progmem but
s1 will be in stack memory
s2 will be in static memory ?
If those statements are inside a function, for S1 the compiler will ignore the PROGMEM and likely will create the array on the stack (actual treatment will depend on how it is used and compiler optimizations), S2 will be stored in program memory (and will need to be accessed as such).
1 Like
J-M-L
March 10, 2022, 11:40am
4
answer depends also on the type of "Arduino" board you use
gcjr
March 10, 2022, 1:03pm
5
PROGMEM suggests the string is stored in flash along with the code and requires the use of pgm_read_word_near() to read it
const char is required for predefined string values and may be initialized in RAM at startup
static prevents global access when defined with file scope. not sure of it's relevance for const char within a function
within a function, static variables such as int is not maintained on the stack and it's values is persistent between executions of the function
Arduino UNO
so ,
inside a funsction
const char s1[] PROGMEM = "I AM PREDATOR"; //in RAM in stack ?
static const char s2[] PROGMEM = "UNSEEN COMBATANT"; //in Flash in static?
global scope
const char s1[] PROGMEM = "I AM PREDATOR"; //in Flash in static ?
static const char s2[] PROGMEM = "UNSEEN COMBATANT"; //in Flash in static ?
1 Like
J-M-L
March 10, 2022, 2:43pm
7
easiest way for you to try is to print s1 and s2. if you see garbage, then they are in PROGMEM
gcjr
March 10, 2022, 2:56pm
8
you may not realize that the Arduino processors use a Harvard architecture where code and data at accessed on separate buses.
the DSPs i worked had this architecture allowing them to read an instruction along with data in the same cycle or when code was cached, to read a state variable and a constant coefficient at the same time
so reading data from flash cannot be read the same way as data in RAM
Inside a function, see my previous reply.
At global scope, both would be stored in flash (program) memory.
Note that the previous discussion you referenced at the start of this thread was creating a char* in PROGMEM, that pointed to a char array in ram.
1 Like
The disassembly agrees with @david_2018 .
Which indicates a bug in the compiler. The compiler was told to place the data in a Flash section but it ignores the request...
const char s1[] PROGMEM = "I AM PREDATOR";
When that's function-local the result should be a compilation error.
It looks like in local scope,
const char s9[] PROGMEM = "CONST LOCAL PROGMEM";
is treated as
const char s9[] = "CONST LOCAL PROGMEM";
(Not put in PROGMEM).
And:
static const char s10[] PROGMEM = "STATIC CONST LOCAL PROGMEM";
is put in PROGMEM.
For globals, PROGMEM values are put in PROGMEM whether static or not.
char s1[] = "GLOBAL";
const char s2[] = "CONST GLOBAL";
const char s3[] PROGMEM = "CONST GLOBAL PROGMEM";
static const char s4[] PROGMEM = "STATIC CONST GLOBAL PROGMEM";
static const char s5[] = "STATIC CONST GLOBAL";
const char *global_strings[5] = {s1, s2, s3, s4, s5};
void setup()
{
Serial.begin(115200);
delay(200);
char s7[] = "LOCAL";
const char s8[] = "CONST LOCAL";
const char s9[] PROGMEM = "CONST LOCAL PROGMEM";
static const char s10[] PROGMEM = "STATIC CONST LOCAL PROGMEM";
static const char s11[] = "STATIC CONST LOCAL";
const char *local_strings[5] = {s7, s8, s9, s10, s11};
for (int i = 0; i < 5; i++)
Serial.println(global_strings[i]);
for (int i = 0; i < 5; i++)
Serial.println(local_strings[i]);
}
void loop() {}
GLOBAL
CONST GLOBAL
M
STATIC CONST GLOBAL
LOCAL
CONST LOCAL
CONST LOCAL PROGMEM
⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮6
STATIC CONST LOCAL
no I know when it in ram and when in flash , Q more about stack/static
How the Hell can you have a stack in PROGMEM?
1 Like
You can't. It looks like the PROGMEM designation is ignored for local variables that aren't 'static'.
"Without it the foos exist on the stack."
SwissPotato:
Can you explain me why is the "static" keyword important here ?
Without it the foos exist on the stack.
Actually, I thought that all PROGMEM var (in FLASH) were static, since the FLASH isn't written during the execution of the program
We are discussing a C++ program which means C++ rules still apply. Without the static they are automatic variables which puts them on the stack.
I have wrote at the beginning - question came up after reading the topic at link
1 Like
system
Closed
September 10, 2022, 8:42am
16
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.