Is something wrong with the compiler ?

Henry Spencer henry at zoo.toronto.edu
Sat Sep 29 01:17:23 AEST 1990


In article <BURLEY.90Sep27013312 at world.std.com> burley at world.std.com (James C Burley) writes:
>   Sure you know what the results should be?  The second expression is
>   assigning to `a' twice, which has unpredictable effects...
>
>Hmm, I thought it was ok -- doesn't "=" serve as a "sequence point" or
>some such thing, and since the lhs of the outer = is so simple it can't
>be "evaluated" before the rhs has been, and the rhs contains the other
>=, one is guaranteed the latter = is performed before the former, right?

Wrong.  Assignment is not a sequence point.  Furthermore, taking the
value of an assignment is no guarantee that the assignment has been
performed.  It's quite legal for the compiler to save up all the assignments
and do them at the end of the expression, if that happens to be more
convenient.  The only constraint is that the update to the lhs must occur
somewhere between the previous and the next sequence point.

In the absence of intervening sequence points (*some* operators do cause
them), the result of modifying an object's value more than once in the same
expression is specifically undefined.  It might work.  It might fail.  It
might work only when the Moon is full.  It might use your modem to place
a call to the Pentagon and request an airstrike on your house. :-)
-- 
Imagine life with OS/360 the standard  | Henry Spencer at U of Toronto Zoology
operating system.  Now think about X.  |  henry at zoo.toronto.edu   utzoo!henry



More information about the Comp.lang.c mailing list