I have some C++ code like the following:
#include <memory>
void do_memory()
{
std::unique_ptr<int[]> ptr = std::make_unique<int[]>(50);
int* ptr2 = new int[50];
delete[] ptr2;
}
In the first case case, I'm creating a unique pointer to an int array, and the second case, I'm allocating a raw int array. Both arrays get cleaned up by the time the scope is left. Playing around with this code (e.g. https://godbolt.org/g/c3gEfV), I see that the optimized assembly for these two sets of instructions are different because make_unique performs value initialization (specifically, it seems to set all the values of the allocated array to 0). So make_unique is introducing a bit of unwanted overhead.
What is the recommended way to allocate a unique_ptr to an array (as in the above) without automatic value initialization? I have experimented with e.g.
std::unique_ptr<int[]> ptr = std::unique_ptr<int[]>(new int[50]);
but in my application I also have the restriction that I don't know the size of my array at compile time, so I'd like to not have to allocate any arrays with a (compile-time) constant size.