2 bytes allocated problems

RW rwmaillists at googlemail.com
Thu Feb 25 17:00:54 UTC 2010


On Wed, 24 Feb 2010 20:55:15 +0100
Dag-Erling Smørgrav <des at des.no> wrote:

> Andrey Zonov <andrey.zonov at gmail.com> writes:
> > Dag-Erling Smørgrav <des at des.no> writes:
> > > Pointers have no boundareis in C.
> > And how free() finds that the need to release?
> 
> That is a very simple question with a very complicated answer.

A simple answer is that information about the amount of memory
allocated is stored at the time it's allocated. And free() can find that
data from the pointer.

I don't know how it works in FreeBSD, but a traditional method is to
put the metadata just before the block of memory returned by malloc -
so free() can find it with a simple fixed offset.

Note that the allocation algorithm is free to allocate more memory than
it needs to, it doesn't need to record what malloc actually asked for,
and it doesn't need to know about data structures that are put in that
memory. 


More information about the freebsd-hackers mailing list