FAQ 12.06 Should the pointer returned from new Fred() be checked to see if it is NULL?
Because of this, the if test in the following example is considered bad form since it increases code size, increases code complexity, and increases testing costs, yet it adds no value (remember, exceptions guarantee that p will never be NULL). #include <new> using namespace std; class Fred { }; void sample() throw(bad_alloc) { Fred* p = new Fred(); if (p == NULL) { <-- 1 // ... } // ... } int main() { sample(); }
C programmers please note that this behavior is very different from the way out-of-memory is handled by malloc(). To be safe, every call to malloc() has to be followed by an explicict if test to see if malloc() returned NULL. |