Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!brutus.cs.uiuc.edu!wuarchive!wugate!uunet!virtech!cpcahil From: cpcahil@virtech.UUCP (Conor P. Cahill) Newsgroups: comp.lang.c Subject: Re: swap(x,y) Message-ID: <1065@virtech.UUCP> Date: 24 Aug 89 01:38:16 GMT References: <1061@virtech.UUCP> <17504@ut-emx.UUCP> Organization: Virtual Technologies Inc Lines: 30 In article <17504@ut-emx.UUCP>, pmaniac@walt.cc.utexas.edu (Noah Friedman) writes: > To prevent this problem, why not cast the arguments as doubles? > > void swap(double *x, double *y) > { *x += *y; *y = *x - *y; *x -= *y; } > > func() > { > int a, b; > swap((double *) &a, (double *) &b); > } This would not work, and would probably cause a core drop, because 1. The swap will always modify sizeof(double) bytes. a is sizeof(int) bytes and usually the two are not the same. Casting an address does NOT effect the data the address refers to. 2. The data at &a will probably not meet the standard for representation as a double and would likely cause a floating point exception. 3. If the data items were doubles, you could still overflow in the first statement. -- +-----------------------------------------------------------------------+ | Conor P. Cahill uunet!virtech!cpcahil 703-430-9240 ! | Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 | +-----------------------------------------------------------------------+