malloc() and sizeof

Jym Dyer jym at wheaties.ai.mit.edu
Tue Apr 4 07:15:18 AEST 1989


In article <9969 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
>In article <510 at lakesys.UUCP> chad at lakesys.UUCP (Chad Gibbons) writes:
>>	struct foo tmp = (struct foo *)malloc(sizeof *tmp);
>
> Sizeof comes in two flavors, sizeof(type) and sizeof object.  In the
> latter case the object-expression is not evaluated, only its type is
> used.  Therefore the above usage is perfectly legitimate.

It should be pointed out that object can be in parentheses.  This fact
 is not lost on those who prefer to use sizeof like a function instead
  of the operator that it is.

Also, the example has a typo in it.  The `tmp' variable must be declared
 as a pointer to struct foo, not as a struct foo.
  <_Jym_>



More information about the Comp.lang.c mailing list