-
-
Notifications
You must be signed in to change notification settings - Fork 7k
b=b++; It depends on the version of software #3570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
|
This is a classic C/C++ "gotcha". b=b++ is "undefined behaviour". http://stackoverflow.com/questions/4968854/is-i-i-truly-a-undefined-behavior. Undefined behaviour means the result depends on the compiler, as you have found, and both 1.0.5 and 1.6.5 are producing correct results, according to the standard. You should change your code - to increment b, use one of "b++;" "b = b + 1;" or "b +=1". |
Wow, thanks @bobc , learning something new every day. |
It appears a large amount of reversal is occurring due to optimizations. Your case has popped up many times, however I have recently seen ordering differences in the way the compiler evaluates function parameters and even allocation of global addresses (#3061), which are implementation defined. The key is to avoid assumptions. If you're unsure, stack overflow can be a big help, however the latest (free) standard can be accessed here : |
Thanks everyone for the clear explanation! |
This program with the version 1.0.5, b increases the value by 1, and with the version 1.6.5, b=0 all time.
int b=0;
void setup() {
Serial.begin(9600);
}
void loop() {
b=b++;
Serial.println(b);
delay(100);
}
The text was updated successfully, but these errors were encountered: