This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/58540] Incorrect warning message for '*=' statement and different results based on optimization


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;


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]