Proper way to create unique_ptr that holds an allocated array

C++LinuxGccC++11Unique Ptr

C++ Problem Overview


What is the proper way to create an unique_ptr that holds an array that is allocated on the free store? Visual studio 2013 supports this by default, but when I use gcc version 4.8.1 on Ubuntu I get memory leaks and undefined behaviour.

The problem can be reproduced with this code:

#include <memory>
#include <string.h>

using namespace std;

int main()
{
	unique_ptr<unsigned char> testData(new unsigned char[16000]());

	memset(testData.get(),0x12,0);

	return 0;
}

Valgrind will give this output:

==3894== 1 errors in context 1 of 1:
==3894== Mismatched free() / delete / delete []
==3894==    at 0x4C2BADC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3894==    by 0x400AEF: std::default_delete<unsigned char>::operator()(unsigned char*) const (unique_ptr.h:67)
==3894==    by 0x4009D0: std::unique_ptr<unsigned char, std::default_delete<unsigned char> >::~unique_ptr() (unique_ptr.h:184)
==3894==    by 0x4007A9: main (test.cpp:19)
==3894==  Address 0x5a1a040 is 0 bytes inside a block of size 16,000 alloc'd
==3894==    at 0x4C2AFE7: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3894==    by 0x40075F: main (test.cpp:15)

C++ Solutions


Solution 1 - C++

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);

Solution 2 - C++

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 );

Solution 3 - C++

A most likely better way would be to use std::vector<unsigned char> instead

#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<unsigned char> testData(0x12, 0); // replaces your memset
    // bla    
}

The advantage is that this is much less error-prone and gives you access to all kinds of features such as easy iteration, insertion, automatic reallocation when capacity has been reached.

There is one caveat: if you are moving your data around a lot, a std::vector costs a little more because it keeps track of the size and capacity as well, rather than only the beginning of the data.

Note: your memset doesn't do anything because you call it with a zero count argument.

Solution 4 - C++

unsigned int size=16000;
std::unique_ptr<unsigned char[], std::default_delete<unsigned char[]>> pData(new unsigned char[size]);

Solution 5 - C++

Seems like a goofup, i will explain what i mean

class Object {
private :
	static int count;
public :
	Object() {
		cout << "Object Initialized " << endl;
		count++;
	}
	~Object() {
		cout << "Object destroyed " << endl;
	}
	int print()
	{
		cout << "Printing" << endl;
		return count;
	}
};

int Object::count = 0;

int main(int argc,char** argv)
{
    // This will create a pointer of Object
	unique_ptr<Object> up2 = make_unique<Object>();  
	up2->print();
    // This will create a pointer to array of Objects, The below two are same. 
	unique_ptr<Object[]> up1 = std::make_unique<Object[]>(30);
	Object obj[30];
	cout << up1.get()[8].print();
	cout << obj[8].print();

    // this will create a array of pointers to obj. 
		unique_ptr<Object*[]> up= std::make_unique<Object*[]>(30);
        up.get()[5] = new Object();
        unique_ptr<Object> mk = make_unique<Object>(*up.get()[5]);
        cout << up.get()[5]->print();
     
        unique_ptr<unique_ptr<Object>[]> up3 =  std::make_unique<unique_ptr<Object>[]>(20);
        up3.get()[5] = make_unique<Object>();

	return 0;
}

Objective of the post is that there are hidden small subtle things you need to understand. Creating array of objects is same as object array of unique_ptr. It will make difference only when you pass it in the argument. Creating array of object pointers of unique_ptr is also not very useful. So only below two you need to use in most scenarios.

unique_ptr<Object> obj;
//and 
unique_ptr<unique_ptr<Object>[]>= make_unique<unique_ptr<Object>[]>(20);

Solution 6 - C++

Probably something like the following?

using namespace std;

int size = get_size();
int const init_value = 123;

unique_ptr<int[]> p = make_unique<int[]>(size)
fill(p.get(), p.get() + size, init_value);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlauwView Question on Stackoverflow
Solution 1 - C++juanchopanzaView Answer on Stackoverflow
Solution 2 - C++galop1nView Answer on Stackoverflow
Solution 3 - C++TemplateRexView Answer on Stackoverflow
Solution 4 - C++Competent BitView Answer on Stackoverflow
Solution 5 - C++Raghavendar ReddyView Answer on Stackoverflow
Solution 6 - C++bruinView Answer on Stackoverflow