casting void pointers

James Davies jrbd at craycos.com
Wed Apr 24 02:13:45 AEST 1991


In article <4256 at rwthinf.UUCP> berg at marvin.e17.physik.tu-muenchen.de (Stephen R. van den Berg) writes:
>Roger A. Hurwitz writes:
>>Even K&R 2d states that an explicit cast should be used with malloc(),
>>in another part of the book (p. 142), but I am having trouble reconciling
>>that with the above language.
>
>malloc does not need the cast, any compiler complaining about that is
>braindamaged (or not ANSI compliant :-)

In fact, the cast can actually be damaging.  If you forget to declare
malloc on a 16-bit-int machine like a PC, but then cast the returned value,
most compilers will assume you know what you're doing and quietly strip off
the high 16 bits of your 32-bit pointer.  (I once spent two days finding
a bug like this...)



More information about the Comp.lang.c mailing list