Sequence Points, Swapping Vars (Was Re: Is...wrong with the compiler?)
James C Burley
burley at world.std.com
Sat Sep 29 18:57:36 AEST 1990
In article <189 at thor.UUCP> scjones at thor.UUCP (Larry Jones) writes:
In article <BURLEY.90Sep27013312 at world.std.com>, burley at world.std.com (James C Burley) writes:
> > a = ( int )(( unsigned ) ( a = ~0 ) >> 1 ));
>
> 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?
No, '=' is not a sequence point. The question in this case is not when
the lhs gets evaluated, but when the actual storage of the rhs into the
lhs is done. A compiler would be completely within its rights to store
the final result into a and then overwrite it with ~0.
----
Larry Jones UUCP: uunet!sdrc!thor!scjones
SDRC scjones at thor.UUCP
2000 Eastman Dr. BIX: ltl
Milford, OH 45150-2789 AT&T: (513) 576-2070
Yep, we'd probably be dead by now if it wasn't for Twinkies. -- Calvin
I was wrong, indeed: = is not a sequence point, and thanks to several people
who pointed it out.
However, I did finally look it up in the standard and now I'm confused.
Apparently, other assignment operators (such as &=) are not sequence points,
either.
The reason this confuses me is the other thread(s) I've seen posting solutions
such as "a^=b^=a^=b" (or some such thing) for the problem of swapping integer
variables. If ^= also is not a sequence point, isn't it valid for a
conforming C compiler to write into a the result of XORing the original values
of a and b, instead of the final value (the original value of b)? It was
this discussion I was thinking about when I originally posted my mistaken
impression that = is a sequence point. Or is "a^=b^=a^=b" a nonstandard
solution (in addition, perhaps, to being an inappropriate one)?
James Craig Burley, Software Craftsperson burley at world.std.com
More information about the Comp.lang.c
mailing list