Mail Archives: djgpp/1998/01/14/13:52:03
On Wed, 14 Jan 1998, Noam Rotem wrote:
> Can anyone explain to me the process of freeing a dynamic
> allocation? (I mean the process in memory, by the OS). What
> does *actualy* happen when I try to free a non dynamic
> allocation, or use a pointer to the middle of an allocation
> to free it? I know it is undefined, but why? What does the
> computer do when I use 'free'?
`malloc' usually reserves a few bytes more than you request, and writes
the size of the chunk in those extra bytes. When you pass to `free' a
pointer to something other than the original pointer returned by
`malloc', it uses the place where those extra bytes were supposed to be
stored to know how large the chunk was, and then overwrites it with some
stuff to mark that chunk as unused.
So passing such pointers to `free' means that you invite it to overwrite
memory in an unpredictable place.
- Raw text -