Are persistent variables not widely used? I couldn't find much info about them online or in the index of my C textbook - the Art and Science of C.
Anything you can share about them, especially their scope and example declaration would be helpful. I'm guessing to declare them you use '开发者_Python百科persistent' as the keyword?
static void foo( void ) {
persistent unsigned int width = 5;
}
This is the only other helpful reference I could find: “Persistent variables keep their state when the board is turned off and on, when main is run, and when system reset occurs. Persistent variables will lose their state when code is downloaded as a result of loading or unloading a file.” http://www.newtonlabs.com/ic/ic_5.html#SEC9
thanks!
Interactive C (the link you provided) provides the persistent
keyword, but that is not standard C. Particularly since guarantees like "keep their state when the board is turned off and on, when main is run, and when system reset occurs".
persistent
is provided with the Interactive C compiler and works with dedicated hardware, Motorola chip in this case, storing the variable value in non-volatile memory to achieve persistence over restarts.
Interactive C is a C compilation environment for many Motorola 6811 based robots and embedded systems. Originally developed for the MIT LEGO Robot Design Contest (6.270), Interactive C has enjoyed widespread distribution and use. Interactive C's claim to fame is its interactivity: users can type in expressions and have them compiled on the fly and run immediately, rather than waiting for lengthy compile and download cycles. IC currently supports the 6.270, the HandyBoard and the RugWarrior and RugWarrior Pro. source.
To achieve variable persistence in a local scope (e.g. function), use the static
keyword.
The keyword you want is static
in local (not global) context.
The context thing is important:
#include <stdio.h>
static int foo;
int main(int argc, char **argv){
//...
}
Here static
means that foo
has file scope (i.e. is not extern
).
Whereas in
char *strtok(char *str, char *sep){
static char *last;
//...
}
last
is persistent between calls to strtok
.
All that said, they are rarely used because they are rarely useful, and are totally unacceptable in a multi-threaded context (where they are a race condition waiting to happen).
As jkerian mentioned, persistent variables keep their state when the board is turned off and on ... but are excluded by the C startup code (which is compiler dependent) because the only memory that can be used for this, is the EEPROM.
Like other variables, persistent variables stored in volatile memory lose content on a power lost. But this kind of variable is useful on device with FRAM. FRAM is an acronym for ferroelectric RAM whitch is a non-volatile memory that can hold data even after it is powered off. Note: on CCS in COFF mode, uninitialized variables weren't set to 0 (to save startup time) and therefore were always persistent.
I often use Persistent variables in my PIC MCU software. For instance, I would define an integer array for holding error codes, that I would want to retain after a reset (whether hardware, instruction, watchdog etc.). In addition I would define an integer variable for checking the integrity of all the persistent data. When the program starts I would compare the contents of the check variable with a particular value. This could be any pre-defined value, but I would choose something that the ram is unlikely to power-up with, such as 0xA5F0. If the check variable contains this value, then I can assume all the other persistent variables have retained their data, and move on. However, if the check variable does not compare with 0xA5F0, then I assume all the other persistent variables contain invalid data, so I would initialise them all, and set the check variable to 0xA5F0.
精彩评论