Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 977
davewy Avatar asked Mar 08 '23 07:03

davewy


1 Answers

If you really must, just write your own function:

template <typename T>
std::unique_ptr<T> make_unique_uninitialized(const std::size_t size) {
    return unique_ptr<T>(new typename std::remove_extent<T>::type[size]);
}

Avoid the temptation to create the unique_ptr directly:

std::unique_ptr<T[]>(new T[size])  // BAD

Because this is not exception-safe in general (for all the usual reasons you use make_unique in the first place - consider function calls with multiple arguments and exceptions thrown about).

like image 131
GManNickG Avatar answered Apr 29 '23 19:04

GManNickG