Recommended way to make std::unique_ptr of array type without value initialization?

Viewed 2528

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.

2 Answers

C++20 update

C++20 introduces std::make_unique_for_overwrite, which works just like std::make_unique, except that it does default-initialization instead of value-initialization.

Related