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
When an exception is thrown out of a function the function does not complete and does not return anything, at the same time control flow jumps to the exception handler and even if the function returned, the assignment to the receiving pointer would not be executed.
The same goes for the throwing version of new
, if it throws, then the pointer would maintain the same value it had before the expression, which might be NULL or any other value.
On the other hand, if you use new (std::nothrow) X;
then the call to new
will not throw, and failure will be indicated by a NULL value being returned (and assumingly assigned to a pointer)