Pages: [1]   Go Down
Author Topic: Can variables just vanish after a while?  (Read 819 times)
0 Members and 2 Guests are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
All of the frustration of programming, PLUS the dangers of electricity!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Chris Spurgeon
TWITTER: @chrisspurgeon

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 533
Posts: 26951
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
All of the frustration of programming, PLUS the dangers of electricity!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Chris Spurgeon
TWITTER: @chrisspurgeon

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6146
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 533
Posts: 26951
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
All of the frustration of programming, PLUS the dangers of electricity!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Chris Spurgeon
TWITTER: @chrisspurgeon

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6146
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 300
Posts: 26218
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not?
Logged

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

CO, USA
Offline Offline
God Member
*****
Karma: 5
Posts: 711
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

... it is poor civic hygiene to install technologies that could someday
facilitate a police state. -- Bruce Schneier

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 480
Posts: 18722
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Pages: [1]   Go Up
Jump to: