Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ihnp4!chinet!dag From: dag@chinet.UUCP (Daniel A. Glasser) Newsgroups: comp.lang.c Subject: Re: swap macro -- beware -- oh yeah ? Message-ID: <2154@chinet.UUCP> Date: 29 Jan 88 06:21:41 GMT References: <8801200422.AA19420@decwrl.dec.com> <3061@killer.UUCP> <1182@ark.cs.vu.nl> Reply-To: dag@chinet.UUCP (Daniel A. Glasser) Organization: Chinet - Public Access Unix Lines: 30 In article <3061@killer.UUCP> jfh@killer.UUCP (John Haugh) writes: [a ^= b; b ^= a; a ^= b;] >What if, A and B are aliases? What do you mean by aliases? Are they both the same storage or just the same value? > > let's say, A = B = 7. okay, they are similar values but different storage. > after a ^= b, both a and b == 0. Nope, a == 0, b still == 7 > then, after both b ^= a and a ^= b (the second time), a and b > are STILL == 0. No again, after b ^= a, a == 0 and b ==7, and now after a ^= b, a == 7 and b == 7. I think you are being too quick to attack. This dosn't hold up, of course if &a == &b, but depending on how your compiler handles constant expressions, and assuming you don't use pointers, a macro like the following: #define swap(a,b) if(&(a)!=&(b)){(a)^=(b);(b)^=(a);(a)^=b} might evaluate to the same code as without the "if", since &a and &b will usually be constant expressions (even if they are autos or paramters, there relative addresses will be known). As I said, it depends on your compiler. -- Nobody at the place where I work Daniel A. Glasser knows anything about my opinions ...!ihnp4!chinet!dag my postings, or me for that matter! ...!ihnp4!mwc!dag ...!ihnp4!mwc!gorgon!dag One of those things that goes "BUMP!!! (ouch!)" in the night.