swap(x,y)
Conor P. Cahill
cpcahil at virtech.UUCP
Thu Aug 24 11:38:16 AEST 1989
In article <17504 at ut-emx.UUCP>, pmaniac at 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 |
+-----------------------------------------------------------------------+
More information about the Comp.lang.c
mailing list