I was wondering if anyone could help me to sort out a bit of confusion I have with unique pointers. Consider the following code:
// some class "complex" defined here
unique_ptr<complex> p(new complex);
unique_ptr will call
delete on the pointer it owns when it goes out of scope. So you're deleting memory allocated by
delete instead of
delete. That's undefined behavior and it's pretty much pointless to try to reason any further why your code doesn't behave the way you expect it to.
But what might be happening is calling
delete on that pointer means only the first
complex object in the array is being destroyed (meaning its destructor gets called). The destructor will not be called for the remaining 255 objects, but the memory occupied by the entire array is still freed.
I think your expectation is for your program to use more and more memory and finally die when the
new allocation fails, but if the memory occupied by the array is being released, that won't ever happen.
The correct way to handle an array with a
unique_ptr is to use the partial specialization for array types
unique_ptr<complex> p(new complex); // ^^
Or if your standard library implements
std::make_unique (C++14), then use
auto p = std::make_unique<complex>(256);