type punning

Guy Harris guy at rlgvax.UUCP
Sun Feb 10 05:15:27 AEST 1985


> Note the lack of cast on h in Put_Tail.

Oh, JHFC, Gary, RTFM.  K&R says that the coercion of 0 to a null pointer
of the appropriate type is automatic in what amount to all situations where
the compiler can figure this out.  Stop talking about the lack of casts
in statements like "if (!h)"; they never were required and never will be.

> Now in the modules where we deal primarily with buffers, Get_Head is
> declared as BUFP Get_Head; where we deal with channels, CHANP Get_Head.
> And where widgets are used Get_Head is of type WIDGETP. And so on.
> Lint will go bonkers over this!

OK, where's the good reason (other than fingers tired of typing casts)
for type punning?  This whole thing can be expressed the same way with
casts (if constantly typing casts get in your way, define some macros).
ANSI Standard C would permit you to define Get_Head as returning "void *",
in which case the casts wouldn't be necessary to silence "lint".  The
compiler would also know enough to generate correct code.

I suspect with a language designed with type inheritance in mind, what you
specify could be stated in a much more straightforward fashion (buffers,
channels, widgets, etc. inherit the doubly-linked-list nature of lists),
and be written in a type-safe fashion.  I prefer to let the computer handle
all the tedious details, like all correctness checks doable by computer,
generation of optimal code, etc., which it can deal with better (given that
machines, unlike humans, don't perform less poorly when bored, tired, etc.)
than I can.

	Guy Harris
	{seismo,ihnp4,allegra}!rlgvax!guy



More information about the Comp.lang.c mailing list