I recently updated from 1.0.1 to 1.6.1 and noticed an odd issue when running an application. I eventually tracked it down to a series of functions sharing a reference parameter. Below is a simple example to demonstrate the difference. There is no reason I did this other than it seemed convenient and saved a line of code. I have no problem splitting the code to two lines, just curious as to what changed with the compiler or if there is a basic programming concept I am missing.
This will print '5' in 1.0.1 and '0' in 1.6.1.
#include <Arduino.h>
void setup() {
Serial.begin(9600);
int a = 0;
func2(func1(a), a);
}
void loop() { }
int func1(int &b) {
b = 5;
return 0;
}
void func2(int c, int d) {
Serial.println(c + d);
}
You cannot guarantee the order the parameters are evaluated in. As 'a' is passed by value to the second parameter the change to the reference has no effect (parameters have been evaluated in reverse order).
This is most probably the effect of an optimization, and as a result, it has most likely removed the 'int a' altogether.
Looks like you have discovered a second case where 'undefined' behavior produces different results on 1.0.X and 1.6.X. The first case was "value = value++;" which increments value in 1.0.x but not in 1.6.x.
Best not to use constructs with 'undefined behavior' (or even 'implementation-defined behavior').
Pyro - Your explanation makes sense.
John - I always known division by zero and out of bounds arrays as undefined behavior, but never considered something like this until today. Now I know better.