C++ standard says that operator new must throw std::bad_alloc exception
when memory allocation fails.
Consider the following test application:
#include <iostream>
#include <limits>
int main(){
try{
char * ptr = new char[std::numeric_limits<int>::max()];
if (!ptr) std::cout << "NULL returned!" << std::flush;
else std::cout << "Miracle!" << std::flush;
}
catch(std::bad_alloc e){
std::cout << "Exception: bad_alloc!" << std::flush;
}
return 0;
}
When this code runs on Linux Red Hat (3.2.3) exception is thrown.
On Windows with VC 7.1 there is no exception and NULL is returned by operator new.
How to fix this problem.
The only way to fix this is to link a Windows application with thrownew.obj which is provided with Visual Studio.
So by default Windows application will not throw std::bad_alloc exception.
By the way VS 6.0 has another problem. When set_new_handler is used operator new(std::nothrow) returns NULL in DEBUG version and it still throws an exception in release.