Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 22 Sep 1997 06:09:37 +0000 (GMT)
From:      Terry Lambert <tlambert@primenet.com>
To:        ac199@hwcn.org
Cc:        sef@kithrup.com, hackers@FreeBSD.ORG
Subject:   Re: Bug in malloc/free (was: Memory leak in getservbyXXX?)
Message-ID:  <199709220609.XAA15857@usr07.primenet.com>
In-Reply-To: <Pine.BSF.3.96.970921223229.215A-100000@ppp6431.on.sympatico.ca> from "Tim Vanderhoek" at Sep 21, 97 10:35:52 pm

next in thread | previous in thread | raw e-mail | index | archive | help
> 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.

...with the caveat that the OS is a memory overcommit OS, such that
the next call may fail because of another process.

> However, I believe the actual wording is sufficiently ambigous that
> this debate has been fought long and hard in other forums.

Including this one, about 7 months ago...

> Anything which is this controversial can be implemented either way
> and be right. 
> 
> Besides, there ain't no standard which can tell me that free() can't
> return memory to the OS.

Wrong.  Technically, the reallocation does not have to check its return
value, and may assume the allocation never fails, according to the
standard.

The question to ask of the code is whether it *needs* the memory, or
merely *wants* the memory.  If it merely *wants* the memory, then a
failed allocation is a recoverable condition, since it checks the
return value of the function.


If you want to be entirely technical about it, a malloc only has to
return a pointer that becomes valid when used.  That means that it
can return a pointer to an unmapped page, and map the page in when
the allocated area is actually written (or read -- though that's not
too useful).

The allocation of backing store for the allocated region could fail
at that time, as well, technically.  So a malloc can return a non-failure
indicator, yet have failed.

Ah, the mysteries of memory overcommit!  8-).


					Terry Lambert
					terry@lambert.org
---
Any opinions in this posting are my own and not those of my present
or previous employers.



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