Possible duplicate:
Inconsistent value calculations (aka sequence points)
Undefined Behavior and Sequence Points
Operator Priority and Evaluation Order
I'm still trying to circle my head as the following expression leads to undefined behavior:
a = a++;
After searching SO about this, I found the following question:
Difference between sequence points and operator priority? 0_o
I have read all the answers, but I am still having difficulty with the details. One answer describes the behavior of my above code example as ambiguous in terms of changing a . For example, this may result in one of the following:
a=(a+1);a++; a++;a=a;
What exactly makes modification a ambiguous? This is due to processor instructions on different platforms, and how can the optimizer use undefined behavior? In other words, it seems undefined due to the generated assembler?
I do not see the reason the compiler uses a=(a+1);a++; He just looks freaky and doesn't make much sense. What will the compiler have to make it behave this way?
EDIT:
Just to be clear, I understand what is happening, I just don’t understand how it can be undefined when there are rules about the priority of the operator (which essentially determines the evaluation order of the expression). In this case, the assignment is the last, so first you need to evaluate the value of a++ in order to determine the value for assignment a . So I expect that a first changes during the increment after the fix, but then gives a value to reassign a (second modification). But the rules for operator precedence seem to make the behavior very clear to me, I can’t find where there is any kind of “fly-in” to have undefined behavior.
void.pointer
source share