swap() macro

Root Boy Jim rbj at icst-cmr
Fri Jul 4 05:36:58 AEST 1986


	In article <2225 at umcp-cs.UUCP> I replied:
	>I have here a C program that effects call-by-name and does
	>indeed perform a swap:
	
	What I failed to consider, of course, is the classic problem with
	call by name: arrays.  Watch what happens when I `swap' `i' and
	`a[i]', using an expanded form of the swap function (this is
	necessary to avoid compiler dependencies in this particular case).

Okay, so at least Chris and I are talking about the same thing.
	
	Ah well, at least I caught my error myself. . . .

Ah, but why not fix it too? Who says you have to evaluate f1 & f2 each
time you reference them? 
	
	swap(f1, f2)			swap(f1, f2)
	int *(*f1)(), *(*f2)();		int *(*f1)(), *(*f2)();
	{				{
		int t1, t2;		int t1, t2, *a1, *a2;
	
		t1 = *(*f1)();		t1 = *(a1 = (*f1)());
		t2 = *(*f2)();		t2 = *(a2 = (*f2)());
		*(*f1)() = t2;		*a1 = t2;
		*(*f2)() = t1;		*a2 = t1;
	}				}

By definition you say? Whaddya want, swapping or call by name? To the
outside world, swap is atomic, so who cares what happens inside?
This way, t2 is unneeded as well.

	-- 
	In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
	UUCP:	seismo!umcp-cs!chris
	CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu

Now recite with me the Hacker's Prayer:

	Our father, UART in heaven, Hallowed Call by Name ...

	(Root Boy) Jim Cottrell		<rbj at icst-cmr.arpa>
OMNIVERSAL AWARENESS?? Oh, YEH!! First you need 4 GALLONS of JELL-O
and a BIG WRENCH!!... I think you drop th'WRENCH in the JELL-O as if
it was a FLAVOR, or an INGREDIENT...  ...or...I...um...WHERE'S the
WASHING MACHINES?



More information about the Comp.lang.c mailing list