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)
Smart pointers for T[] If you want to create a unique_ptr , you can write: class Object { }; // unique_ptr auto ptr = std::make_unique<Object>(); auto intPtr = std::make_unique<int>(); // or shared_ptr auto shared = std::make_shared<Object>(); auto intShared = std::make_shared<int>();
It can be assigned: class owner { std::unique_ptr<someObject> owned; public: owner() { owned=std::unique_ptr<someObject>(new someObject()); } };
A typical declaration for an array in C++ is: type name [elements]; where type is a valid type (such as int, float ...), name is a valid identifier and the elements field (which is always enclosed in square brackets [] ), specifies the size of the array.
Use unique_ptr when you want to have single ownership(Exclusive) of the resource. Only one unique_ptr can point to one resource. Since there can be one unique_ptr for single resource its not possible to copy one unique_ptr to another. A shared_ptr is a container for raw pointers.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With