malloc() and sizeof

Guy Harris guy at auspex.auspex.com
Tue Apr 4 15:07:49 AEST 1989


>	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?

Well, in the December 7, 1988 dpANS, kt gives as an example of "sizeof",
on page 46:

	double *dp = alloc(sizeof *dp);

so I presume they intend it to be usable, and even that they don't think
it's too weird.  As for more formal guarantees, we have on page 45:

	3.3.3.4 The "sizeof" operator

	...

	...The size is determined from the type of the operand, which is
	not itself evaluated.

so I'd say that "dp" doesn't have to point to anything reasonable for
"sizeof *dp" to be valid.



More information about the Comp.lang.c mailing list