Thread: Checking new for an error?

  1. #1
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    Checking new for an error?

    I read on the internet the other night that there isn't much you can do if a call to 'new' fails because there isn't enough memory left so the system will probable crash anyway.

    Is this true, or is this really OS dependant? I would think that in DOS when a call no new failed it would probably crash the system because memory would be to low. On the other hand in windows OS's would it not just use the page file and not crash?

    Can anyone straighten me out on this subject?
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Is this true
    Maybe, but not likely. If new fails it could be because it couldn't find a block of consecutive bytes large enough to meet your request. If memory is way low on just about any OS these days it'll just show things down because of extra paging and use of virtual memory. A crash because of low memory is very improbable these days.

    What you should do if new fails is try to recover by doing something like asking for smaller chunks, or work to gracefully terminate with what you have. If possible, you should try to recover first and then die gracefully if you can't, not vice versa. :-)
    *Cela*

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    There's a few things you can do if allocation fails...
    Look into std::set_new_handler(), it takes a pointer to a function which it calls when allocation fails. You can localize all your attempts to recover from a failed alloc inside your handler function.
    The handler function must do one of the following:
    • Make more memory availible
    • Install a different handler
    • Deinstall itself
    • Throw an exception
    • Not return

    On a failed alloc, your handler is called repeatedly(forever) until one of these conditions is met.

  4. #4
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    Thank you both for your quick replies!

    I'll do some tinkering tomorrow and see how that all works.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    new allocates virtual memory not physical memory, at least, it does on a Windows platform. On an NT cored system, each process has a 4GB virtual address space, the pages required being swapped into physical memory on demand.

    If you try to allocate more than 4GB I would expect new to throw a bad_alloc exception as that is the ANSI standard. I have, however, not found a compiler that does that on a PC, new tends to return NULL on an allocation failure, (which it should do if the new(nothrow) variant is used).

    I would certainly not expect the OS to have any problems given sufficient pagefile space.

    If you are trying to allocate more than 4GB of virtual memory, I wonder if a PC is really the right tool for the job!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM