-1

In pgmspace.h, PROGMEM is defined as __ATTR_PROGMEM__, which is defined as __attribute__((__progmem__)). AFAIK, __attribute__ is GNU only. Since C++ 11, we have the attribute specifier sequence.

I can't use [[__progmem__]] instead of PROGMEM. The error is

warning: '__progmem__' attribute directive ignored [-Wattributes]

Shouldn't that work equally?

Why would I want to do that?

  1. Clarity: When I first read code that used PROGMEM, I thought that this is the variable name (being a constant, we have a conding guideline that it should be upper case, which matched). Reading on, I found the real variable name, which confused me. Putting it into square brackets would immediately have made clear that it's an attribute and not a name.
  2. Portability: [[ ]] is Standard C++, whereas __attribute__ is not.

1 Answer 1

0

Maybe I should just read the complete documentation...

Under Standard Attributes, we see the attributes that come with the C++ Standard. __progmem__ isn't one of them.

Following the example for syntax 2, I tried [[gnu::__progmem__]], which compiles. So this helps with Clarity, but since it now has the gnu namespace, it doesn't help with Portability.

2
  • 1
    Re “it doesn't help with Portability”: According to the documentation of PROGMEM, “These functions are an attempt to provide some compatibility with header files that come with IAR C, to make porting applications between different compilers easier.” I doubt PROGMEM could be a lot more portable than that. How many C compilers (let alone C++) support the AVR architecture? How many of those support keeping constants in flash memory? Commented Oct 4, 2023 at 15:39
  • And the macro approach has the nice effect that you can provide a macro without replacement text for systems that do not support such attributes, for example if you simulate on a PC. Commented Nov 6, 2023 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.