Cross-compiler way to handle memory allocation failure ?

Discussion in 'C++' started by Koen, Jun 24, 2003.

  1. Koen

    Koen Guest

    Hi!
    What is the method you use to handle memory allocation failure in C++
    using the "new" operator?
    I used to only check if a NULL pointer is returned after the "new" call,
    but that's probably only working in MSVC++.
    I also read about the "bad_alloc" exception, but that doesn't seem to be
    thrown by MSVC++.
    Now, one could of course say "don't use MSVC++", but I'd rather like to
    hear a good solution for being able to handle memory allocation failure
    in a cross-compiler way (we're using MSVC++ on windows and gcc on
    linux). Any ideas anyone?
    Is installing a new handler the only way to be sure?
    Koen
     
    Koen, Jun 24, 2003
    #1
    1. Advertising

  2. "Koen" <> wrote in message news:<bd9ftc$lvf$>...
    > Hi!
    > What is the method you use to handle memory allocation failure in C++
    > using the "new" operator?
    > I used to only check if a NULL pointer is returned after the "new" call,
    > but that's probably only working in MSVC++.
    > I also read about the "bad_alloc" exception, but that doesn't seem to be
    > thrown by MSVC++.


    Not in the last two versions. You have to go back to the 6.0 series,
    which predates the standard. Both VC7.0 and VC7.1 get it right.

    > Now, one could of course say "don't use MSVC++", but I'd rather like to
    > hear a good solution for being able to handle memory allocation failure
    > in a cross-compiler way (we're using MSVC++ on windows and gcc on
    > linux). Any ideas anyone?
    > Is installing a new handler the only way to be sure?


    I believe installing a new handler is also buggy on VC6. Upgrading from
    VC6 to VC7 is relatively painless; i've done it on more than one project.
    As others mentioned, new(nothrow) is an option.

    For windows, this often is a bit of a non-issue though. Windows will
    swap to death before failing, the user will probably kill off the
    program first. The only case I know that fails fast enough to matter is
    running into the 2Gb limit, if your PC has more than 2Gb RAM. I don't
    know if that's implemented properly in VC6, though.

    Regards,
    --
    Michiel Salters
     
    Michiel Salters, Jun 25, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    494
  2. Ken
    Replies:
    24
    Views:
    3,946
    Ben Bacarisse
    Nov 30, 2006
  3. chris
    Replies:
    6
    Views:
    1,031
    chris
    Oct 28, 2005
  4. vectorizor

    Memory allocation failure

    vectorizor, May 22, 2007, in forum: C Programming
    Replies:
    1
    Views:
    449
    Barry Schwarz
    May 22, 2007
  5. Saeed Amrollahi
    Replies:
    10
    Views:
    718
    Kevin P. Fleming
    Jan 8, 2011
Loading...

Share This Page