|
|
[16.5] Do I need to check for NULL after p = new Fred()?
No!
If your compiler doesn't support (or if you refuse to use) exceptions, your
code might be even more tedious:
Fred* p = new Fred();
if (p == NULL) {
cerr << "Couldn't allocate memory for a Fred" << endl;
abort();
}
In C++, if the runtime system cannot allocate sizeof(Fred) bytes of memory
during p = new Fred(), a bad_alloc exception will be thrown.
Unlike malloc(), new never returns NULL!
Therefore you should simply write:
|
|