[Insight-developers] is operator new returning NULL ?

Luis Ibanez luis.ibanez at kitware.com
Tue Dec 11 08:26:22 EST 2007



Not that we are intentionally picking on VS6,... but...

VS6 will not throw exceptions when the "new" operator
fails to allocate memory:


           http://www.codemaestro.com/articles/14


"Operator new Surprises in VC6

The following item is a must for every Visual C++ 6.0 programmer which
uses operator new. The default behavior of operator new in Microsoft
Visual C++ 6.0 in case of an error is to return NULL, in total contrast
to the standard which explicitly states that new raises a bad_alloc
exception upon failure. This behavior can lead to many unpleasant
surprises, especially when trying to write portable code.

Here are some painful facts to feast your eyes with after you have
written so much “error free” code with operator new in VC6:

* Unlike VC6, Newer versions of Visual Studio conform to the exception
throwing standard.

* The STL is written according to the standard, therefore it assumes an
exception will be thrown upon failure.

* Platform independent code may end up on other machine types that
conform to the standard.


--


So the trick of asking for NULL is (unfortunately) still necessary.

Another workaround is to register a custom error handler for the
new operator... as described in the link above.



    Luis



-------------------------
Mathieu Malaterre wrote:
> Hi,
> 
>   The other day I was reading through some C code ported to C++ in
> Code/IO namely the Siemens/GE4/GE5 stuff and found the following piece
> of code:
> 
>   GEImageHeader *hdr = new GEImageHeader;
>   if(hdr == 0)
>     {
>     RAISE_EXCEPTION();
>     }
> 
>   AFAIK operator new never returns NULL, but throw an exception...well
> on a standard conforming compiler. so the question is: is the code
> simply copy/paste from C code or are there some (VS6 ?) buggy compiler
> that do not throw exception ?
> 
> thanks,
> 


More information about the Insight-developers mailing list