malloc() and sizeof

Chad Gibbons chad at lakesys.UUCP
Mon Apr 3 18:08:36 AEST 1989


	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));

The style I have seen used recently around here has been this:

	struct foo tmp = (struct foo *)malloc(sizeof *tmp);


	I ran tests of this on several different systems, and they all
compiled and worked fine...however, this seems to be a poor programming
practice at best, and a shoestring at worse.  Anyone have any comments
about this?  I would assume it wouldn't be a good idea to use it, but
then again, you never know.
-- 
D. Chadwick Gibbons, chad at lakesys.lakesys.com, ...!uunet!marque!lakesys!chad



More information about the Comp.lang.c mailing list