get size of malloc'd object

David desJardins desj at brahms.BERKELEY.EDU
Tue Jun 24 11:09:53 AEST 1986


   I can't believe all of this discussion about clever tricks to try to guess
how malloc does alignment, or what the maximum alignment boundary is, or
whatever.  No matter what you do along these lines, it is going to be possible
to devise a perfectly valid C implementation in which it will fail.
   It seems obvious that the solution to the whole problem is to define a
structure which contains two fields: the pointer to memory returned by malloc,
and an int (or long int, or whatever) which contains the size of the block
requested from malloc.  This is guaranteed to work, as opposed to the many
alternate implementations that have been proposed and are guaranteed to fail
on at least some architectures.
   Note that in a strongly-typed language you would have had to do the right
thing from the beginning...

   -- David desJardins



More information about the Comp.unix mailing list