Reputation: 126
I am trying to understand what would occur if I failed to allocate memory in a constructor for an object which is itself dynamic.
Example:
Say I am dynamically allocating an object and I am checking whether the object was allocated:
Object* object = new(nothrow) Object();
if (object == nullptr) { // handle stuff }
Now what if the constructor of the Object()
allocates a pointer array like so new int[n]
.
My question is if the pointer array allocation fails. Will that result in object
being set to nullptr
? Or is it a distinctly separate case to handle
Any help is appreciated! (Also please no smart pointer / STL alternatives. I want to know how this works)
Upvotes: 0
Views: 84
Reputation: 141628
In this case an exception is thrown.
The new(nothrow)
only means that failure to allocate storage for the object will not throw, it does not place constraint on the object constructor (nor constructors of subobjects).
Upvotes: 4