Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!purdue!haven!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: malloc() and sizeof Message-ID: <16687@mimsy.UUCP> Date: 3 Apr 89 14:11:42 GMT References: <510@lakesys.UUCP> Distribution: usa Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 21 In article <510@lakesys.UUCP> chad@lakesys.UUCP (Chad Gibbons) writes: > I have seen a somewhat strange style of coding a malloc() call on >some systems. Usually, given struct foo, you would execute the call such >as this: > > struct foo tmp = (struct foo *)malloc(sizeof(struct foo)); No: rather, `struct foo *tmp = ' >The style I have seen used recently around here has been this: > > struct foo tmp = (struct foo *)malloc(sizeof *tmp); Perfectly legal (given `struct foo *tmp' rather than `struct foo tmp'). The argument to sizeof is examined only to find its type; any compiler that actually tries to find `*tmp' during program execution has a severe bug (one on the order of computing 1|2 as -1 instead of 3, or 1||2 as -1 instead of 1). -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris