-
-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Comments
The same goes for objects too. |
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. |
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. |
@ElectricRCAircraftGuy I added the statement as you suggested. If it is ok for you please close this issue |
@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? |
@ElectricRCAircraftGuy it is there before tue see also in a paragraph "Note" written in bold |
@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:
and
|
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.
This also reflects what is widely used in a majority of examples/forum questions I've seen. |
@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:
The following code WILL work, even if locally defined within a function:
|
thanks! |
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
The text was updated successfully, but these errors were encountered: