Re: Darwin malloc()-ing a few large blocks vs. many many small blocks



In article <1127346337.479886.247580@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
eva.monsen@xxxxxxxxx wrote:

> Hi there, I have encountered the following problem. I'm writing a C++
> program that possibly exceeds the machine's memory capacity, and I need
> it to check for memory allocation errors. Normally I'd check for a NULL
> returned from new or malloc(), and catch bad_alloc exceptions.
>
> Using Darwin on Mac OS X Panther, new/malloc throws a bad_alloc
> exception after allocating several very large blocks.
>
> When allocating lots and lots of small blocks (ie asking for new
> double() over and over), I'd expect the same behavior. However, using
> Darwin, I found that malloc() causes the program to terminate after
> allocating lots and lots of small blocks. There is no option to
> recover. Is this a bug in Darwin, or me?
>
> My suspicion is that Darwin's malloc() keeps track of a table of
> currently allocated blocks, and that for the many-small-blocks case
> that table grows very large and is unchecked. malloc experts, am I
> close?

I see nothing obviously wrong with your code that would cause heap corruption,
so I have to conclude that you have, indeed, uncovered a bug in malloc. I urge
you to report this to Apple, via bugreporter.apple.com.

(A side note that is completely unrelated to your original question: you are
catching your exceptions incorrectly. You should catch all exceptions by
reference, not by value: catch (bad_alloc&) rather than catch (bad_alloc).
Otherwise, your code will behave incorrectly in the presence of derived
exception classes. This particular example doesn't suffer from that but because
new always throws objects whose most-derived class is bad_alloc I thought I'd
mention it in case you were unaware of this issue.)

hth

Ben

--
If this message helped you, consider buying an item
from my wish list: <http://artins.org/ben/wishlist>

I changed my name: <http://periodic-kingdom.org/People/NameChange.php>
.