malloc/free practice - more from the author

Doug Gwyn gwyn at smoke.BRL.MIL
Thu Oct 19 20:36:37 AEST 1989


In article <1989Oct17.033415.16036 at anucsd.oz> bdm at anucsd.oz (Brendan McKay) writes:
>The only alignment requirement made for the value of malloc() is THAT IT
>IS SUITABLE FOR USE *AFTER CASTING*. ... read the standard again.
>Yes, this is maybe just an exercise in pedantry.  Well-written standards
>should survive such exercises, though.

If you had constructive comments on the wording used in the proposed
Standard, you should have made them during the public review phase
when there would have been some chance that we would have performed
some "wordsmithing" to clarify points that may have confused you.

However, since we're being pedantic here, I'll point out that the
Standard does NOT state what you capitalized.  What is does say is,
"The pointer returned if the allocation succeeds is suitably aligned
so that it may be assigned to a pointer to any type of object and
then used to access such an object or array of such objects in the
space allocated ... The pointer returned points to the start (lowest
byte address) of the allocated space."  You appear to have jumped on
the first sentence without considering its context.  The second
sentence I cited makes it clear that the pointer that malloc()
returns is ITSELF already suitably aligned; no conversion is
necessary to ensure that.  The reason we say "may be assigned to a
pointer..." is that the void* returned by malloc(), like any void*,
itself is NOT a pointer to an "object" in the formal C sense,
although it does point to the start of allocated space.

P.S.  As usual, this is not an official X3J11 response.



More information about the Comp.std.c mailing list