View Single Post
Old 09-17-2005, 07:20 AM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Before the days of exception handling, a failure of operator new set the pointer to NULL. The pointer needed to be tested explicitly for NULL to check for failures. These days that is not needed anymore because we have a more neat way of exception handling. But setting the pointer to NULL will not occur anymore.
See how easy it can be:
Code:
#include <iostream>
#include <new> 
using namespace std;

int main()
{
  //Always healthy to initialize pointers with 0.
  unsigned char *c = 0;
  
  
  try
  {
    c=new unsigned char[0x7fffffff];
  }
  catch(const bad_alloc& xa)
  {
    cerr << "ERROR: bad allocation caught." << endl;
    //New compilers will call operator delete() if the constructor throws
    //an exception. So no worries about deletion here.
  }
  
  //Avoid sementation fault. Now it shows why initializing with 0 is handy.
  if(c!=0) //If allocated successfully...
  {
    delete[] c; //...delete pointer.
  }

  return 0;
}
__________________
Valmont is offline   Reply With Quote