Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!bloom-beacon!ai-lab!jym From: jym@wheaties.ai.mit.edu (Jym Dyer) Newsgroups: comp.lang.c Subject: Re: malloc() and sizeof Message-ID: <1460@wheat-chex.ai.mit.edu> Date: 3 Apr 89 21:15:18 GMT References: <510@lakesys.UUCP> <9969@smoke.BRL.MIL> Reply-To: jym@wheaties.ai.mit.edu (Jym Dyer) Organization: Ono-Sendai Lines: 14 In article <9969@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn) writes: >In article <510@lakesys.UUCP> chad@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_>