Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 21 Sep 1997 22:46:26 -0600 (MDT)
From:      Nate Williams <nate@mt.sri.com>
To:        Sean Eric Fagan <sef@kithrup.com>
Cc:        tim@ppp6431.on.sympatico.ca, hackers@freebsd.org
Subject:   Re: Bug in malloc/free (was: Memory leak in getservbyXXX?)
Message-ID:  <199709220446.WAA29021@rocky.mt.sri.com>
In-Reply-To: <199709220258.TAA27605@kithrup.com>
References:  <199709220258.TAA27605@kithrup.com>

next in thread | previous in thread | raw e-mail | index | archive | help
> >> 	char *cp = malloc(100);
> >> 	if (cp) {
> >> 		free(cp);
> >> 		cp = malloc(100);
> >> 	}
> >No, if you want the above code to always result in a non-NULL cp,
> >free() cannot ever return the memory back to the OS.
> 
> Really?  Would you like to explain that idiotic statement?  Back it up with
> some facts, explanations, or justifications?

If between the lines of free(cp) you get a context switch, another
application allocates enough memory to completely use up the VM in the
system, and then the process is switched to again, the second malloc
could fail due to lack of VM.  Again, this is a *very* rare case, but
could happen if free() returns memory to the OS.

> I did not point that out because I thought it was obvious.  And it doesn't
> change my position:  if the first malloc() succeeded, then, even if there is
> no more space available after that first malloc(), the free() and subsequent
> malloc() are required to work.

How can they work if there is no more space after the first malloc()?
There is no guarantee that once you get one byte, you're guaranteed as
many as you want, be it 100 bytes or 100000 bytes.



Nate



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199709220446.WAA29021>