Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Cross-compiler way to handle memory allocation failure ?

Reply
Thread Tools

Cross-compiler way to handle memory allocation failure ?

 
 
Koen
Guest
Posts: n/a
 
      06-24-2003
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


 
Reply With Quote
 
 
 
 
Michiel Salters
Guest
Posts: n/a
 
      06-25-2003
"Koen" <(E-Mail Removed)> wrote in message news:<bd9ftc$lvf$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory allocation failure in map container Saeed Amrollahi C++ 10 01-08-2011 04:54 PM
Memory allocation failure vectorizor C Programming 1 05-22-2007 10:59 PM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
How to handle memory allocation i shared hosting environments? (GC.Collect) jdlwright@gmail.com ASP .Net 3 06-13-2006 05:36 PM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM



Advertisments