Values "privacy" in different functions ?

Hello

I had a strange bug few days ago, I am really astonished about it, and I would like to obtain some other person’s experience / point of views :

Normally this sketch should always output 0.

but in such a situation I noticed the value to be set at an apparently random value, or possibly the same value than in an other function’s private.

void setup() {
Serial.begin(9600);
}

void loop() {DoThis();DoThat();delay(300);}

void DoThis() {
int i=405;
}

void DoThat() {
int i;
Serial.print(i);
}

I solved the problem simply setting a fixed value at function begin. Like this :

void DoThat() {
int i=0;
Serial.print(i);
}

and then the problem was wiped away.

Thank you for sharing your opinion / experience about this !!

Francois

sketch_jul11c.ino (161 Bytes)

The moral is don't use uninitialised variables:

void DoThat() {
  int i;   // uninitialised automatic variable
  Serial.print(i);
}

when you define a variable, "int i", you allocate space for a variable within that scope (within the braces or a file). in your code, the "i" in DoThis() is a different variable than the "i" in DoThat().

void DoThis() {
 int i=405;
}

void DoThat() {
  int i;
  Serial.print(i);
}

Yes, if uninitialised you get whatever happens to be in the memory that becomes associated with the variable when the function is calld.

In this exact case, both local i variables occupy the same exact location on the stack, hence the “coincidence“ that it looks like it is picking up an i from another function.

One could write some wicked confusing code exploiting this.

There are no mysteries in micros, just stuff you don’t (yet) know or see clearly.

a7

If you did not get these helpful warning messages you should go into "Preferences..." and set your "Compiler warnings:" to "All". If your sketch won't compile without warnings you are probably doing something wrong. (In some cases it's a library author that did something wrong and you can't do much about that.)

/Users/john/Documents/Arduino/sketch_jul08a/sketch_jul08a.ino: In function 'void DoThis()':
/Users/john/Documents/Arduino/sketch_jul08a/sketch_jul08a.ino:16:7: warning: unused variable 'i' [-Wunused-variable]
   int i = 405;
       ^
/Users/john/Documents/Arduino/sketch_jul08a/sketch_jul08a.ino: In function 'DoThat()':
/Users/john/Documents/Arduino/sketch_jul08a/sketch_jul08a.ino:22:15: warning: 'i' is used uninitialized in this function [-Wuninitialized]
   Serial.print(i);
   ~~~~~~~~~~~~^~~

Thank you very much for this tip Johnwasse !! :slight_smile: :slight_smile: I did totally ignore the existence for this functionnality.
I turn it on right now.

I've guessed some IDE could help me much better to debug my code. I've hear about VisualBasic, I want to try, hope it's not too complex to use. Can't use it now, I need to upgrade my mac before.

Thanks for all for your opinion too ! The problem, as I am learning alone, I fear some other stuff like this in code. Sometimes the code runs unexpectedly, and surely have a good reason for it, but to be aware of the reason is another matter.