Since this is the standard C/C++ forum, the code would be different. See comments as well for a few tips:
Code:
#include <iostream>
#include <new> //set_new_handler()
#include <cstdlib> //abort()
using namespace std;
//The standard denotes set_new_handler to be defined as:
// typedef void (*ptrF)()
void MyNewHandler()
{
cerr<<"[MyNewHandler] Allocation error!";
//std::abort()... use this as a very last resort.
//Solution: use the standard provided exception handling mechanism for
//allocation failures by calling its appropriate exception.
abort();
}
int main()
{
unsigned char *c;
//Now we install our own handler.
//NOTE: if this handler is called, operator new() will NEVER retrun NULL.
set_new_handler(MyNewHandler);
try
{
c=new unsigned char[0x7fffffff];
}
catch(...)
{
//This will never be shown!
cout <<"[TryMemAlloc] Exception thrown!";
}
cout<<" char allocated at address: " << c << endl;
delete[] c;
return 0;
}
The code posted by the OP is not part of the ISO standard.