[Insight-developers] is operator new returning NULL ?

Bill Lorensen bill.lorensen at gmail.com
Tue Dec 11 09:07:27 EST 2007


Luis,

Yesterday I added a test for ImportImageComtainer called
Testing/Code/Common/itkImportContainerTest. As part of the test I apply the
Reserve method with a very large memory request. The test expects an
exception to be thrown.

And... it appears that VS6 catches it:
http://www.itk.org/Testing/Sites/dash13.kitware/Win32-vs60/20071211-0100-Nightly/Results/__Testing_Code_Common_itkImportContainerTest.html

BTW, Win64-Vista-vs8-Release does not throw an exception:
http://www.itk.org/Testing/Sites/redwall.kitware/Win64-Vista-vs8-Release/20071211-0100-Nightly/Results/__Testing_Code_Common_itkImportContainerTest.html

Interesting...

Bill

On Dec 11, 2007 8:26 AM, Luis Ibanez <luis.ibanez at kitware.com> wrote:

>
>
> 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,
> >
>  _______________________________________________
> Insight-developers mailing list
> Insight-developers at itk.org
> http://www.itk.org/mailman/listinfo/insight-developers
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.itk.org/mailman/private/insight-developers/attachments/20071211/fb6a5d05/attachment.html


More information about the Insight-developers mailing list