Can variables just vanish after a while?

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 } he nuts?

PS> Obviously, I'll try this myself, but I'm at work and don't have a board with me here.

Well, theoretically int x is only declared within void setup, so yes.

If int x was before void setup, it would be global and always there.

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."

1200000 decimal = 0x124F80

So you may need to declare a variable as type unsigned long delayTime = 1200000UL; // or 0x124F80UL and then use that in delay(delayTime);

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!

You Can't have garbage collection on a processor platform that doesnt have a memory controller.

Why not?

CrossRoads: Well, theoretically int x is only declared within void setup, so yes.

But then you get a compile error, because it's out of scope in the rest of your sketch. It couldn't disappear, because it isn't there to start with.

chrisspurgeon: he nuts?

He's pulling your leg. The variable can't disappear. Are you saying it takes on some other value, than 10? What other value?

This reminds me of some other posts about problems someone has "heard of" but can't actually post. Let's stick to reproducible problems, not rumours.