Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!purdue!haven!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn ) Newsgroups: comp.lang.c Subject: Re: malloc() and sizeof Message-ID: <9969@smoke.BRL.MIL> Date: 3 Apr 89 19:11:08 GMT References: <510@lakesys.UUCP> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 14 In article <510@lakesys.UUCP> chad@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..