[Bug c/58540] Incorrect warning message for '*=' statement and different results based on optimization
redi at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Thu Sep 26 16:45:00 GMT 2013
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58540
--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to bernardwidynski from comment #2)
> Logic requires that the "=" be executed before the "*="
That's not the problem, the assignment are sequenced, but the evaluation of the
operands is not. The standard says:
"The side effect of updating the stored value of the left operand is
sequenced after the value computations of the left and right operands. The
evaluations of the operands are unsequenced."
Which means the compiler can evaluate the left-most u.y first, when it's
uninitialized, then it can assign (x+r) to u.y, then it can multiply the result
by the uninitialized value. Or it can evaluate the operands in different
orders, producing a different result, hence the result is undefined.
Just write it like this and avoid the problem:
u.y = x + r;
u.y *= u.y;
More information about the Gcc-bugs
mailing list