get size of malloc'd object

Frank Adams franka at mmintl.UUCP
Tue Jun 24 23:56:16 AEST 1986


In article <2216 at peora.UUCP> jer at peora.UUCP writes:
~>determine alignment by difference between consecutive malloc'ed objects

>you're not guaranteed that the two consecutive mallocs will give
>consecutive blocks of memory -- they certainly won't be likely to after
>some other mallocs and frees have been done.  It's hard to see a situation
>where the initial allocations wouldn't be consecutive, but it's not
>guaranteed, and that's sufficient reason to have doubts.

(1) If you are using the "buddy system"[1] for storage allocation, and the
total storage available is not a power of two, initial allocations might
well not be consecutive.

(2) Some version of malloc have header information about the allocated block
immediately before the block.  For such versions, the distance between blocks
always includes at least one such header.

(3) Storage may be allocated and freed in the process of starting the
program, before your code gets control.

(4) Storage for all programs may be allocated from a single heap, so that
other tasks would have done malloc's and free's before (or even between)
yours.

In short, this is completely unreliable.

Frank Adams                           ihnp4!philabs!pwa-b!mmintl!franka
Multimate International    52 Oakland Ave North    E. Hartford, CT 06108

[1] For a description of the buddy system, see Knuth, _The_Art_of_Computer
_Programming,_vol._1,_Fundamental_Algorithms_.



More information about the Comp.unix mailing list