Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!rutgers!iuvax!bsu-cs!dhesi From: dhesi@bsu-cs.UUCP (Rahul Dhesi) Newsgroups: comp.lang.c Subject: Re: typeof isn't enough to define swap correctly Message-ID: <1106@bsu-cs.UUCP> Date: Fri, 4-Sep-87 18:08:50 EDT Article-I.D.: bsu-cs.1106 Posted: Fri Sep 4 18:08:50 1987 Date-Received: Sat, 5-Sep-87 20:48:40 EDT References: <557@rocky.STANFORD.EDU> Reply-To: dhesi@bsu-cs.UUCP (Rahul Dhesi) Organization: CS Dept, Ball St U, Muncie, Indiana Lines: 34 Keywords: a can of worms by any other name In article <557@rocky.STANFORD.EDU> andy@rocky.UUCP (Andy Freeman) quotes Walter Bright's swap macro: >> #define swap(a,b) { typeof(a) tmp; tmp = a; a = b; b = tmp; } and says, Even given typeof, this definition of swap is buggy...I've never figured out how to define a C swap macro that works - I'm willing to believe it is impossible. Way back, when Algol's call-by-name parameter passing method was invented, they never realized what a can of worms it would be. One of these worms is the proven impossibility of writing a general swap function in Algol. (I think I'm thinking of Algol68, but it might be true of earlier versions too.) As far as I can tell, the swap macro is the equivalent of an Algol subprogram that uses call-by-name, so it's not surprising that a generized swap macro seems to be impossible. The trouble occurs in things like swap(i, a[i]); because the macro changes i before it changes a[i]. An unexpected effect is: a[a[i]] = something; (P.S. Niklaus Wirth has a joke about how his name is pronounced: you can call him by name, pronounced Veert, or call him by value, pronounced Worth.) -- Rahul Dhesi UUCP: {ihnp4,seismo}!{iuvax,pur-ee}!bsu-cs!dhesi