Kenneth Brody
5/9/2011 6:06:00 PM
On 5/9/2011 11:32 AM, Keith Thompson wrote:
> Angel<angel+news@spamcop.net> writes:
>> On 2011-05-08, kamalasan baireddy<kamalasanb@gmail.com> wrote:
>>> How free() knows to delete memory of same created by malloc() ?
>>
>> malloc() and friends are programmed to "remember" which block(s) they
>> give out, usually by adding a small piece of metadata just before or
>> after the block it gives out. free() can find this metadata by pointer
>> arithmetic.
> [...]
>
> A minor point: I don't think the information can be stored *after* the
> memory block. free() is given a pointer to the beginning of the
> allocated block. Without knowing how big the block is, it wouldn't be
> able to find the metadata at the end of it.
>
> There could be *some* metadata stored after the block, as long as
> there's some other mechanism to find it -- but it would be more likely
> to be clobbered by clumsy user code than metadata stored before the
> block.
Yes, I have seen debugging implementations that store metadata after the
block (in addition to before the block), and verify the integrity of that
data at certain times, such as when you free() that block.
--
Kenneth Brody