Operator new with nothrow option still throws exception
Asked Answered
H

3

7

There is such code:

#include <iostream>

int main(){
    for(;;){
        int* ptr = new (std::nothrow) int;
        if(ptr == 0){
            std::cout << 0 << std::endl;
            break;
        }
    }
    std::cin.get();
    return 0;
}

However, this program still throws std::bac_alloc exception, altough new is called with std::nothrow parameter. This program is compiled in Visual C++ 2010. Why the exception is thrown?

Edit:

Using g++ on Windows from mingw, everything works ok.

Hemi answered 26/9, 2011 at 12:35 Comment(8)
This program should not compile. It's a pity it does. You should add #include <new>. Anyway, what version of VC++ are you using?Clothing
Well, it does . Version from 2010 year.Hemi
It also compiles on gcc. What the hell made me believe it would be a good idea to actually run it?Venepuncture
@pmr: You'd set ulimit to something small before running this, of course :-) Works fine, by the way.Drill
Perhaps the << to cout is throwing. What happens if you remove all the iostream stuff and return instead of break?Monamonachal
It works fine on ideone -- see ideone.com/gZK6F.Buttonhole
@fizzer: no change, it still throws exceptionHemi
@scdmb: Try the code I posted to ideone. What happens?Buttonhole
S
1

I just ran your sample from VC2010. It is not new(nothrow) that throws, but __security_check_cookie.

Speechless answered 26/9, 2011 at 13:41 Comment(2)
Hmm, that appear to be a compiler bug then.Cerium
The link is broken, please explain what was thereHamburg
C
7

0 has to be formatted as "0". That's going to take a few bytes; I'll bet that's the cause. Put a breakpoint on std::bad_alloc::bad_alloc and you will know for sure.

Cerium answered 26/9, 2011 at 12:57 Comment(2)
Or put a try-catch block around the new thing and a try-catch block around the entire function to know if it really is the new that fails or not.Perception
Even better, check the option to break on a C++ exception being thrown.Speechless
S
1

I just ran your sample from VC2010. It is not new(nothrow) that throws, but __security_check_cookie.

Speechless answered 26/9, 2011 at 13:41 Comment(2)
Hmm, that appear to be a compiler bug then.Cerium
The link is broken, please explain what was thereHamburg
P
-3

This explains why it is still throwing and how you can make it not throw. It seems nothrow is just ignored.

If you still want the non-throwing version of new for the C Runtime Library, link your program with nothrownew.obj. However, when you link with nothrownew.obj, new in the Standard C++ Library will no longer function.

I found an quite in depth article about this but it's kinda dated (VC 6) but maybe the problem still persists. BTW VC ignores all throw() specifications of functions.

When Operator new(std::nothrow) Throws an Exception Anyway

Pm answered 26/9, 2011 at 12:49 Comment(2)
That page seems to talk about the "normal" operator new (new X as opposed to new (std::nothrow) X), whereas the question is specifically about the latter.Dropsical
That refers specifically to the "regular" new. See blogs.msdn.com/b/stevejs/archive/2005/11/01/487776.aspx for the explanation.Cerium

© 2022 - 2024 — McMap. All rights reserved.