malloc() and sizeof

Doug Gwyn gwyn at smoke.BRL.MIL
Tue Apr 4 05:11:08 AEST 1989


In article <510 at lakesys.UUCP> chad at lakesys.UUCP (Chad Gibbons) writes:
>The style I have seen used recently around here has been this:
>	struct foo tmp = (struct foo *)malloc(sizeof *tmp);
>compiled and worked fine...however, this seems to be a poor programming
>practice at best, and a shoestring at worse.

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.  As to
whether it is better or worse than the alternative style, there
don't seem to be really strong arguments on either side.  I personally
prefer sizeof(type) since to me the other form is just a corruption of
this fundamental definition, but I'm sure other programmers disagree.
It doesn't seem to be worth arguing about..



More information about the Comp.lang.c mailing list