[m]allocation question

John F. Haugh II jfh at rpp386.cactus.org
Sat Sep 29 10:20:44 AEST 1990


In article <11666:Sep2603:44:3190 at kramden.acf.nyu.edu> brnstnd at kramden.acf.nyu.edu (Dan Bernstein) writes:
>In article <UaxqffS00Vtq8akksB at andrew.cmu.edu> ghoti+ at andrew.cmu.edu (Adam Stoller) writes:
>>         t = (struct node *) malloc(sizeof *t);
>
>The argument of sizeof isn't evaluated. Only its type matters. This is
>correct code, and perhaps easier to understand than the other obvious
>version.

I've been meaning to beg to differ, but looking at the code fragment,
the type of "t" is unknown, and thus the size of the allocation.  You
might argue that "t" is "obviously" (struct node *), but that would be
kinda silly - there is no declaration of "t" laying around to stare
at, and besides I'm just in the mood to argue.

I always try to "say what I mean" when I do mallocs and other naturally
obscure stuff (and I'm getting to the point, so bear with me) and would
prefer to do

	t = (struct node *) malloc (sizeof (struct node));

when it is a (struct node) that I want.  Otherwise, I would much
rather do

	t = (typeof t) malloc (sizeof *t);

but I don't recall "typeof" being part of the standard, so I'll have to
hope someone in alt.futures.c screams and dances about this one ;-)
-- 
John F. Haugh II                             UUCP: ...!cs.utexas.edu!rpp386!jfh
Ma Bell: (512) 832-8832                           Domain: jfh at rpp386.cactus.org
"SCCS, the source motel!  Programs check in and never check out!"
		-- Ken Thompson



More information about the Comp.std.c mailing list