As title says, i know that new throws an exception which can be caught, but what exactly happens to the pointer? it turns NULL? I checked some answers on SO but none explain
If an exception is thrown, then the pointer will not be assigned a new value. If your pointer is scoped inside of the try
block where you do the allocation, or is not scoped in any try
block, then the exception will cause the scope to change such that you can't access the pointer any more. Consequently, there's no need to test the pointer.
If, on the other hand, the pointer is declared in a way where it persists after the allocation fails, then you would need to check the value. For example:
T* ptr = NULL;
try {
ptr = new T();
} catch (std::bad_alloc& ) {
/* ... handle error */
}
// Have to test ptr here, since it could still be NULL.
Hope this helps!