Date: Wed, 24 Jun 1998 11:49:44 +0300 (IDT) From: Eli Zaretskii To: Zixyer S Qwerty cc: djgpp AT delorie DOT com Subject: Re: free() In-Reply-To: <19980623.131350.7759.0.zixyer@juno.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk 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.