Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!munnari.oz.au!bunyip.cc.uq.oz.au!brolga!uqcspe!cs.uq.oz.au!grue From: grue@cs.uq.oz.au (Frobozz) Newsgroups: comp.lang.c Subject: Re: macros and semicolons Message-ID: <2180@uqcspe.cs.uq.oz.au> Date: 27 Jun 91 06:27:09 GMT References: <1991Jun24.213932.595@otago.ac.nz> <1991Jun24.144701.8479@world.std.com> <160662@pyramid.pyramid.com> Sender: news@cs.uq.oz.au Reply-To: grue@cs.uq.oz.au Lines: 35 In <160662@pyramid.pyramid.com> markhall@pyrps5.pyramid.com (Mark Hall) writes: >NO macro will work for a swap. You suffer from the call-by-name rule >which undid ALGOL in this case. Consider the expansion of SWAP(i,a[i]): > int c; c = i; i = a[i]; a[i] = c; Something like: #define SWAP(a, b) {int *ap = &a, *bp = &b, t; t = *ap; *ap = *bp; *bp = t; } removes the problems associated with the call by name (there are other problems present, but the call by name is gone). Each argument gets evaluated exactly once. There is no longer any call by name problems. This code will not work for register variables, but they do not suffer from the kind of problems above (if one parameter is a register variable, you can use it instead of it's pointer in the above and it will still work). Register variables are only an optimisation anyway, so they are not all that important --- I have a naive trust in modern compiliers to get the register allocation right. Pauli seeya Paul Dale | Internet/CSnet: grue@cs.uq.oz.au Dept of Computer Science| Bitnet: grue%cs.uq.oz.au@uunet.uu.net Uni of Qld | JANET: grue%cs.uq.oz.au@uk.ac.ukc Australia, 4072 | EAN: grue@cs.uq.oz | UUCP: uunet!munnari!cs.uq.oz!grue f4e6g4Qh4++ | JUNET: grue@cs.uq.oz.au --