macros and semicolons

Frobozz grue at cs.uq.oz.au
Thu Jun 27 16:27:09 AEST 1991


In <160662 at pyramid.pyramid.com> markhall at 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 at cs.uq.oz.au
Dept of Computer Science| Bitnet:       grue%cs.uq.oz.au at uunet.uu.net
Uni of Qld              | JANET:           grue%cs.uq.oz.au at uk.ac.ukc
Australia, 4072         | EAN:                          grue at cs.uq.oz
                        | UUCP:           uunet!munnari!cs.uq.oz!grue
f4e6g4Qh4++             | JUNET:                     grue at cs.uq.oz.au
--



More information about the Comp.lang.c mailing list