View Single Post
Old 09-17-2005, 06:29 AM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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.
__________________
Valmont is offline   Reply With Quote