You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
b=0 from 1.6.5 is the correct result, b++ should return the value before the increment takes place. Not quite sure why it wouldn't do the same in 1.0.5, sounds like it would be a way too notorious issue to be a bug in the compiler.
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".
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.
Activity
carlosperate commentedon Jul 22, 2015
b=0
from 1.6.5 is the correct result,b++
should return the value before the increment takes place. Not quite sure why it wouldn't do the same in 1.0.5, sounds like it would be a way too notorious issue to be a bug in the compiler.bobc commentedon Jul 22, 2015
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".
carlosperate commentedon Jul 22, 2015
Wow, thanks @bobc , learning something new every day.
Chris--A commentedon Jul 23, 2015
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 :
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf
facchinm commentedon Jul 23, 2015
Thanks everyone for the clear explanation!