macro inefficiency

Ronald BODKIN utility at quiche.cs.mcgill.ca
Tue Oct 2 06:41:00 AEST 1990


In article <1990Oct1.002217.4945 at craycos.com> pmk at craycos.com (Peter Klausler) writes:
[that
	#define swap (a,b) { typeof (a) tmp; tmp = a; a = b; b = tmp; }
fails for if (..) swap(a,b); else ..., while changing the original to
>	#define swap (a,b) do { typeof (a) tmp; tmp = a; a = b; b = tmp; } \
>				while (0)
	works].
		Quite right (also pointed out to me in email), although
mine was better than the original, and if one's compiler doesn't optimize
away conditionals with fixed values, it produces better code.  However,
the idea of using do { ... } while (0) to allow a statement that is
closed by a semicolon is a good one.
		Ron



More information about the Comp.lang.c mailing list