what can happen, if 'new' fails (out of memory)?

what can happen, if 'new' fails (out of memory)?

Post by Martin Henn » Sun, 17 Feb 2002 11:31:28



  In almost every book about C++ I read, they
  say that, if 'new' fails, it returnes a pointer to
  NULL or they throw an exception (std:.bad_alloc).

  Unfortunately, I compiled my application on a
  outdated woody snapshot with gcc 2.95.3 and
  I was not able to prevent my code from crashing.

  When the system memory was exhausted, the
  programm was aborted and a message 'out of
  memory' appeared on the console.

  I encapsulated the dangerous code in a 'try'
  block and I catched 'std::bad_alloc' as well
  as every other exception (catch(...)) but this
  didn't help.

  Testing the pointer if it is NULL didn't help
  either.

  Is there a third way of what could happen
  if 'new' fails? I only know the NULL- and the
  exception thing.

  On my machine at home (2.95.4, woody)
  an exception is thrown and can be caugth.
  But on that other woody machine, I couldn't
  handle this situation.

  Martin

--

http://www.martinhenne.de
http://www.aachenprogrammingclub.de
http://apcstudio.sf.net

 
 
 

what can happen, if 'new' fails (out of memory)?

Post by Robert J. Hanse » Sun, 17 Feb 2002 14:26:59


Quote:>   Is there a third way of what could happen if 'new' fails? I only know
>   the NULL- and the exception thing.

GCC's support for certain C++ features is ... well, I would say "lacking",
but that'd be complimentary.  GCC's support for certain C++ features is
nothing short of absolutely pathetic.  I've seen exceptions which could
not fail to be caught go un-caught, for instance, and that sounds as if it
might be what's happening to you.

I've found that STLport (http://www.stlport.org) helps GCC's C++
conformance considerably.  While it's not germane to the exception problem
you're facing, if you do any sort of serious C++ work I'd strongly suggest
ditching GCC's STL as soon as possible.

STLport is free software, available under an MIT/X11-style license.

 
 
 

what can happen, if 'new' fails (out of memory)?

Post by ctr2s.. » Sun, 17 Feb 2002 16:31:22



>   In almost every book about C++ I read, they
>   say that, if 'new' fails, it returnes a pointer to
>   NULL or they throw an exception (std:.bad_alloc).
[...]
>   When the system memory was exhausted, the
>   programm was aborted and a message 'out of
>   memory' appeared on the console.

Is it possible that the kernel is killing your application?  When the
system runs out of memory, the kernel will start killing processes and
generally starts with the one using the most memory... which will
probably be your application.

You will probably see something like this on the console:

  kernel: Out of memory, killing `yourprocessname' (pid 1234)

It should also appear in `dmesg'.

--

"I woke up this morning and realized what the game needed: pirates,
pimps, and gay furries."  - Rich "Lowtax" Kyanka

 
 
 

what can happen, if 'new' fails (out of memory)?

Post by Kasper Dupon » Sun, 17 Feb 2002 18:22:29



>   In almost every book about C++ I read, they
>   say that, if 'new' fails, it returnes a pointer to
>   NULL or they throw an exception (std:.bad_alloc).

>   Unfortunately, I compiled my application on a
>   outdated woody snapshot with gcc 2.95.3 and
>   I was not able to prevent my code from crashing.

>   When the system memory was exhausted, the
>   programm was aborted and a message 'out of
>   memory' appeared on the console.

>   I encapsulated the dangerous code in a 'try'
>   block and I catched 'std::bad_alloc' as well
>   as every other exception (catch(...)) but this
>   didn't help.

>   Testing the pointer if it is NULL didn't help
>   either.

>   Is there a third way of what could happen
>   if 'new' fails? I only know the NULL- and the
>   exception thing.

>   On my machine at home (2.95.4, woody)
>   an exception is thrown and can be caugth.
>   But on that other woody machine, I couldn't
>   handle this situation.

>   Martin

I think this is probably the kernel overcommiting
memory. When memory is requested from the kernel it
will tell that the memory is available. When later
needed the kernel might find that there really was
not enough memory available. In many cases changing
/proc/sys/vm/overcommit_memory does not seem to
help. (I cannot see why that should be so difficult.
When overcommit_memory is true mmap/brk should fail
if there is not enough physical memory or swap.
Shouldn't be hard to keep track of.)

--
Kasper Dupont

 
 
 

1. Can apache/stronghold log when a user 'click outs' of your site?

If I have a link to a URL on another server, can I log when a user
clicks this link? (sort of like HTTP_REFERRER, but the inverse)

Example:

My site is www.A.com and I have a link to www.B.com along with
www.C.com, www.D.com, etc... I want to know if a user clicks on any of
these links.

Is this possible?

Thanks greatly,
Chuck

2. crond dies while startup

3. repeated failed open()'s results in lots of used memory [Was: [Fwd: memory consumption]]

4. CACHEFS..Whats being cached?

5. [2.4] Oops if limit of memory via 'mem=123' failed.

6. dsniff cannot see connections

7. Error msg 'failed to clear shared memory' when detecting second NE2000 card

8. CD-Rom Problems Mounting

9. New: Failed to register 'sysfs' in sysfs

10. 'new' is out of virtual memory

11. Error: Virtual memory exceeded in 'new'

12. Virtual Memory Exceeded in 'New'

13. virtual memory exceeded in 'new'?