Go Down

Topic: Can variables just vanish after a while? (Read 912 times) previous topic - next topic

chrisspurgeon

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.
Chris Spurgeon
TWITTER: @chrisspurgeon

CrossRoads

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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

chrisspurgeon

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.
Chris Spurgeon
TWITTER: @chrisspurgeon

James C4S

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."
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

CrossRoads

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);
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

chrisspurgeon

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!
Chris Spurgeon
TWITTER: @chrisspurgeon

James C4S

You Can't have garbage collection on a processor platform that doesnt have a memory controller.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

justjed


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.
... it is poor civic hygiene to install technologies that could someday
facilitate a police state. -- Bruce Schneier

Nick Gammon


...is 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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up