I guy I know swears that in a sketch he made a variable he declared and set to a value would just vanish if not referenced or used in any way during a time period as short as 10 minutes or so. I was VERY skeptical of this, having never seen anything similar in any of my sketches.
Does anyone have any knowledge of anything like this? He didn't have his sketch code available of course, but he says something as simple as this could trigger the issue...
void setup() {
Serial.begin(9600);
int x = 10;
delay(1200000); // 20 minutes
Serial.println(x); // should print "10";
}
void loop() {
// nothing in here
}
...is he nuts?
PS> Obviously, I'll try this myself, but I'm at work and don't have a board with me here.
Even though when we attempt to retrieve the variable, we're still in setup()?
OK, let's say we DO declare it before setup(), so it's global, he seems to think that if no line of code is executed that references that variable (or deletes it of course, or changes its value) for a good long period of time then when you finally DO try to reference it, it will magically have vanished. I say no way.
Can't wait for the weekend, when I can set up a sketch to run a good 24 hours or so to test this.
If you believe this guy, I have a bridge in San Francisco I can sell you. Good price, very cheap. Lightly used.
No, variables don't "disappear" for no reason. If they did, no computer on earth would function properly. It is much more likely something else in the code mucked with that memory location. For example, a buffer may have over run.
In your example, the line "delay(1200000); // 20 minutes" is a suspect.
1200000 is too big to fit into an int, and the complier may not realize it is actually a long. This could lead to unpredictable behavior. I point this out because a simple mistake like this could easily lead someone to thinking things "just disappeared."
Good catch on the variable overflow, I'll make sure to use an unsigned long.
re the "no reason", I dunno, maybe he's thinking there's some sort of garbage collection or something. Though of course any sort of reasonable garbage collection wouldn't touch the variable in this example either.
Anyway, I was and remain sure that he's out of his mind, at least when it comes to this. Thanks all!