Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator new with nothrow option still throws exception

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.

like image 836
scdmb Avatar asked Sep 26 '11 12:09

scdmb


2 Answers

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.

like image 176
MSalters Avatar answered Nov 08 '22 00:11

MSalters


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

like image 1
Nemanja Trifunovic Avatar answered Nov 07 '22 23:11

Nemanja Trifunovic