(referring to i = x * g(); )
N> I understood the post previous to mine in the thread to say that
N> if x in the above was a constant zero, then even if g would
N> normally be defined (although it modifies i) then it wouldn't be,
N> because the multiplication zero allows the result of x to be
N> discarded, and so for the assignment of 0 to i and the alteration
N> in g to happen "out of order".
No, because the sequence point at the semicolon following the statement
in g() which modifies i means that all side effects have taken place at
that point. The side effect of modifying i can't hang around through
multiple sequence points before being resolved, which is what your
theory requires.
N> So my point was that if the optimiser can see that x will always
N> be zero by this point, then it can make the optimisation, and
N> then we will get undefined behaviour.
Er, no. The C standard does not allow for short-circuit evaluation of
operands aside from those to && and ||. Now, the "as if" rule means
that the optimizer is allowed to do whatever it likes to the code so
long as the actual result is the same as it would be if there were no
optimization.
So even if you wrote i = 0 * g(); explicitly, the translated code would
have to evaluate g() and handle its side effects.
Charlton