malloc/free practice - more from the author

David Adrien Tanguay datanguay at watmath.waterloo.edu
Fri Oct 20 21:07:04 AEST 1989


In article <11340 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
# 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.

I disagree about the second sentence. The first sentence says that the
allocated space must contain the desired object, but there's nothing that
says that the object must start at the beginning of the allocated space.

E.g., suppose sizeof(OBJ) == 8 and it needs even alignment (which is also 
the worst case alignment on this fictional machine), and that an
assignment from void * to OBJ * causes the void * value to be advanced to
the next alignment boundary. malloc(OBJ) can then return address value 9,
with, say, 11 bytes following. The assignment to an OBJ *p gives p a value
of 10, which is suitably aligned. This satisfies the conditions of both
sentences, but p cannot be used as the argument to free or realloc.

David Tanguay



More information about the Comp.std.c mailing list