unions or void * to transmit "generic" pointers

Doug Gwyn gwyn at smoke.BRL.MIL
Thu Sep 14 21:49:46 AEST 1989


In article <6003 at arcturus> evil at arcturus.UUCP (Wade Guthrie) writes:
>I'm curious about whether it is okay to use a void * to transmit pointers
>between functions (when the func's agree on the type, but the at least one
>of the functions needs to be able to handle all sorts of DATA pointers) or
>whether it is more correct/portable to use unions of pointers.  

Any object pointer is guaranteed to be coercable into a generic pointer
(void* for Standard C, char* for older C) or a pointer to a smaller-or-
same-size type with the same or less stringent alignment restrictions,
then converted back again, without loss of significant information.
Thus your example is proper.

However, conversion from one pointer type to another need not be a "free"
operation; the representation may have to be reformatted.  Thus, using a
union can provide a performance advantage, since it bypasses any change
of representation.



More information about the Comp.lang.c mailing list