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>
.



Relevant Pages

  • Re: Whats the substitution of brk system call in POSIX standard?
    ... malloc() and mmap, I guess. ... imagine them being portable to OSes with different memory layouts. ... mallocis a library routine and is originally implemented with brk() ... By allocating from a continous ...
    (comp.unix.programmer)
  • Re: Pointer-based arrays output strange
    ... allocated by an external routine (interfacing C malloc via CHASM). ... Seems to me that you are asking whether that unseen routine is implemented ... Be aware that a Fortran array pointer is nothing particularly close to a C ... The short version is that allocating a Fortran pointer in C is "forbidden" in ...
    (comp.lang.fortran)
  • Re: When to check the return value of malloc
    ... Malcolm McLean wrote, On 19/01/08 10:47: ... Imagine you've got 2GB installed and are allocating 20 bytes. ... The system is stressed and programs crash or terminate for lack of memory once a day. ... The chance of the computer breaking during this period is so so much higher, there is in this case no point checking the malloc(). ...
    (comp.lang.c)
  • Re: How to avoid file I/O error when reading files with stringlist
    ... First, you're catching *all* exceptions, even the ones you didn't even know exist yet. ... You're catching EOutOfMemory, and to handle it, you're *allocating more memory* to display an error message, and then exiting the procedure as though everything is fine. ... Another problem is that you will call CloseFile no matter what -- even if the error occurs while calling AssignFile or Rewrite. ...
    (comp.lang.pascal.delphi.misc)
  • Darwin malloc()-ing a few large blocks vs. many many small blocks
    ... When allocating lots and lots of small blocks (ie asking for new ... Darwin, I found that malloc() causes the program to terminate after ...
    (comp.sys.mac.programmer.help)