Remember the good old C days,
When after memory allocating code,
We had to check the pointer against NULL, just to make sure that the memory has actually been allocated.
int * arr = (int*)malloc(sizeof(int)*10);
if(arr==NULL)
{
// No memory allocated
}
Many people carry this habit from C to C++, which is not all together right.
As in CPP new operator throws an error, "bad_alloc" to be precise , in the case where memory was not allocated.
But if u like living in old days, and insist on comparing the pointer to NULL to check whether memory has been allocated or not. You can use
A * a = new(nothrow) A();
Now you can keep on reliving those times.
On a more serious note, this practise is not recommended in C++, but in some projects developed before C++ introduced "exception handling", this practise has to be continued.
No comments:
Post a Comment