delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/24/04:50:18

Date: Wed, 24 Jun 1998 11:49:44 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: Zixyer S Qwerty <zixyer AT juno DOT com>
cc: djgpp AT delorie DOT com
Subject: Re: free()
In-Reply-To: <19980623.131350.7759.0.zixyer@juno.com>
Message-ID: <Pine.SUN.3.91.980624114503.23442M-100000@is>
MIME-Version: 1.0

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019