Proper way to create unique_ptr that holds an allocated array
Use the array version :
auto testData = std::unique_ptr<unsigned char[]>{ new unsigned char[16000] };
Or with c++14, a better form ( VS2013 already has it ):
auto testData = std::make_unique<unsigned char[]>( 16000 );
Using the T[]
specialisation:
std::unique_ptr<unsigned char[]> testData(new unsigned char[16000]());
Note that, in an ideal world, you would not have to explicitly use new
to instantiate a unique_ptr
, avoiding a potential exception safety pitfall. To this end, C++14 provides you with the std::make_unique
function template. See this excellent GOTW for more details. The syntax is:
auto testData = std::make_unique<unsigned char[]>(16000);