Mail Archives: djgpp/1998/06/24/04:50:18
On Wed, 24 Jun 1998, Zixyer S Qwerty wrote:
> How does free() know how much memory to return to the heap?
`malloc' records the amount in the buffer it allocates, and passes you
the pointer to right after that info. When you call `free', it backs up
to that info, and then knows how large is the buffer.
> Does it have
> to be the originally malloc()ed pointer or can it be a different pointer
> to the memory that was allocated?
You must pass exactly the same pointer as what `malloc' returned. The
above should explain why. If you pass any other pointer, your program
will sooner or later crash.
> I'm writing a linked list and keeping
> pointers to each element around kinda defeats the purpose...
I don't see how. The humanity has been using linked lists for several
decades now, and nobody had any problems with deleting nodes yet. Maybe
if you explained what problems bug you, people here could offer
solutions.
- Raw text -