[comp.lang.c] Sequence Points, Swapping Vars

burley@world.std.com (James C Burley) (09/29/90)

In article <189@thor.UUCP> scjones@thor.UUCP (Larry Jones) writes:

   In article <BURLEY.90Sep27013312@world.std.com>, burley@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@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@world.std.com

karl@haddock.ima.isc.com (Karl Heuer) (09/30/90)

In article <BURLEY.90Sep29045736@world.std.com> burley@world.std.com (James C Burley) writes:
>Or is "a^=b^=a^=b" a nonstandard solution (in addition, perhaps, to being an
>inappropriate one)?

Exactly.  This was pointed out in one of the earlier incarnations of this
thread, but not this time around.

Karl W. Z. Heuer (karl@kelp.ima.isc.com or ima!kelp!karl), The Walking Lint

henry@zoo.toronto.edu (Henry Spencer) (09/30/90)

In article <BURLEY.90Sep29045736@world.std.com> burley@world.std.com (James C Burley) writes:
>... 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...

Yes indeed.

Note that most of the really knowledgeable readers in this group have
seen the variable-swapping nonsense too many times to pay much attention
to the latest round of cutesy solutions to a non-problem.
-- 
Imagine life with OS/360 the standard  | Henry Spencer at U of Toronto Zoology
operating system.  Now think about X.  |  henry@zoo.toronto.edu   utzoo!henry