Skip to main content
2 of 4
edited tags
Juraj
  • 18.2k
  • 4
  • 30
  • 49

How can a function/method determine if a const array passed in is PROGMEM (flash) or not (RAM)?

Is it possible for a function/method to know if a constant array that has been passed in is in flash or RAM?

If I have a method or function that receives a constant array that is in RAM, the array is declared like this: const uint8_t MY_ARRAY[] = { 1, 2, 3, 4 };

gets passed into a method or function like this: MyFunction(MY_ARRAY);

and the method or function can handle it like this:

void MyFunction(const uint8_t *MY_ARRAY) {
     uint8_t secondElement = MY_ARRAY[1];
     ...

However, if the array is in Flash (because it's a constant array, so that's where it should go), then the declaration adds PROGMEM: const uint8_t PROGMEM MY_ARRAY[] = { 1, 2, 3, 4 };

The pass looks the same: MyFunction(MY_ARRAY);

And the function declaration looks the same but it needs to use pgm_read_byte_near to get the correct values back:

void MyFunction(const uint8_t *MY_ARRAY) {
     uint8_t secondElement = pgm_read_byte_near(MY_ARRAY+1);
     ...

How can the receiving method know if the array is in flash (PROGMEM) or RAM (no PROGMEM) so it knows to use pgm_read_byte_near or not? Ideally I'd like a compiler error, but the type is the same (both are const arrays of uint8_t).

If pgm_read_byte_near is used when it shouldn't be, or it's not used when it should be, the results are garbage.

Some relevant questions: How to pass a static const (progmem) array to a function

RAM usage question: PROGMEM vs const vs #define

Storing array in PROGMEM

PROGMEM: do I have to copy data from flash to RAM for reading?

Casey
  • 151
  • 6