Skip to content

Update PROGMEM reference page to mention that variables must be either defined Globally or with the static keyword in order to work w/PROGMEM #2122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ElectricRCAircraftGuy opened this issue Jun 8, 2014 · 11 comments
Assignees
Labels
Component: Documentation Related to Arduino's documentation content
Milestone

Comments

@ElectricRCAircraftGuy
Copy link
Contributor

The Arduino PROGMEM reference page (http://arduino.cc/en/Reference/PROGMEM) should have the following important lines added:


Variables must be either globally defined, OR defined with the "static" keyword, in order to work with PROGMEM.

ex:
The following code will NOT work when inside a function:

PROGMEM char long_str[] = "Hi, I would like to tell you a bit about myself.\n"

...though this line WILL work, even if locally defined within a function:

PROGMEM static char long_str[] = "Hi, I would like to tell you a bit about myself.\n"


For more discussion, and detailed code, see here: http://forum.arduino.cc/index.php?topic=245480

@Chris--A
Copy link
Contributor

Chris--A commented Jul 3, 2014

The same goes for objects too.

@ElectricRCAircraftGuy
Copy link
Contributor Author

Chris--A, do you mean to say that if you define a class, it must either be Globally defined OR have the static keyword in its definition? Or are you saying that during instantiation of an object, it must either be globally instantiated OR have the static keyword? I think you mean the latter, but since I'm new to C++ I'm asking you.

@Chris--A
Copy link
Contributor

Chris--A commented Jul 5, 2014

I guess I was pretty vague, there are two different scenarios using objects, the first is using PROGMEM just like any other variable. It does not need static:

struct Foo{
  int val;
};

Foo f PROGMEM = { 5 };
//...
pgm_read_word( &f.val );

But when PROGMEM is in reference to a class member, then static is required.

struct Foo{
  static const int val PROGMEM = 5;
};

Which is accessed like:

  Foo f;
  pgm_read_word( &f.val );
  //Or
  pgm_read_word( &Foo::val );

On a side note, this information is applicable when using variables marked with EEMEM also.

@agdl
Copy link
Member

agdl commented Jun 12, 2015

@ElectricRCAircraftGuy I added the statement as you suggested. If it is ok for you please close this issue

@agdl agdl self-assigned this Jun 12, 2015
@agdl agdl added this to the Release 1.6.5 milestone Jun 12, 2015
@ElectricRCAircraftGuy
Copy link
Contributor Author

@agdl, I'm looking at the arduino page (http://www.arduino.cc/en/Reference/PROGMEM) but don't see your added statements. Where can I see the documentation where you added them? Has it not been released to the Arduino.cc pages yet?

@agdl
Copy link
Member

agdl commented Jun 14, 2015

@ElectricRCAircraftGuy it is there before tue see also in a paragraph "Note" written in bold

@ElectricRCAircraftGuy
Copy link
Contributor Author

@agdl, I see the change now. I think it didn't show up in my browser before because I checked it only a couple minutes after you made the change, and it needed more time to take effect.

Sorry, one more error in my recommended edit. Please add the keyword "const" in front of both lines of code now. I wrote them before const was required by the version of GCC that Arduino IDE uses, but now it won't compile without const being in front. The code lines should look like this now:

const PROGMEM char long_str[] = "Hi, I would like to tell you a bit about myself.\n"

and

const PROGMEM static char long_str[] = "Hi, I would like to tell you a bit about myself.\n"

@Chris--A
Copy link
Contributor

It seems a more accepted way to use PROGMEM, is before the assignment. It is more concurrent with the actual behavior as it is not a type modifier, but a data attribute.

const static char long_str[] PROGMEM  = "Hi, I would like to tell you a bit about myself.\n";

This also reflects what is widely used in a majority of examples/forum questions I've seen.

@ElectricRCAircraftGuy
Copy link
Contributor Author

@Chris--A, sounds fine with me. @agdl , if you're good with it, let's go with this then:

Please note that variables must be either globally defined, OR defined with the "static" keyword, in order to work with PROGMEM.

The following code will NOT work when inside a function:

const char long_str[] PROGMEM = "Hi, I would like to tell you a bit about myself.\n"

The following code WILL work, even if locally defined within a function:

const static char long_str[] PROGMEM = "Hi, I would like to tell you a bit about myself.\n"

@ffissore ffissore modified the milestones: Release 1.6.5, Release 1.6.6 Jun 15, 2015
@agdl
Copy link
Member

agdl commented Jun 15, 2015

@ElectricRCAircraftGuy @Chris--A done!

@ElectricRCAircraftGuy
Copy link
Contributor Author

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Documentation Related to Arduino's documentation content
Projects
None yet
Development

No branches or pull requests

5 participants