创建包含已分配数组的unique_ptr的正确方法

时间:2014-01-27 09:37:59

标签: c++ linux gcc c++11 unique-ptr

创建包含在免费商店中分配的数组的unique_ptr的正确方法是什么? Visual Studio 2013默认支持这一点,但是当我在Ubuntu上使用gcc版本4.8.1时,我会收到内存泄漏和未定义的行为。

使用此代码可以重现该问题:

#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会给出这个输出:

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

6 个答案:

答案 0 :(得分:67)

使用T[]专业化:

std::unique_ptr<unsigned char[]> testData(new unsigned char[16000]());

请注意,在理想情况下,您不必明确使用new来实例化unique_ptr,从而避免潜在的异常安全陷阱。为此,C ++ 14为您提供了std::make_unique函数模板。有关详细信息,请参阅this excellent GOTW。语法是:

auto testData = std::make_unique<unsigned char[]>(16000);

答案 1 :(得分:25)

使用阵列版本:

auto testData = std::unique_ptr<unsigned char[]>{ new unsigned char[16000] };

或者使用c ++ 14,一个更好的形式(VS2013已经拥有它):

auto testData = std::make_unique<unsigned char[]>( 16000 );

答案 2 :(得分:14)

最好的方法是使用std::vector<unsigned char>代替

#include <vector>
#include <string>

using namespace std;

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

优势在于,它更不容易出错,并且可以访问各种功能,例如轻松迭代,插入,达到容量时自动重新分配。

有一点需要注意:如果您要移动数据,std::vector会花费更多,因为它会跟踪大小和容量,而不仅仅是数据的开头。< / p>

注意:您的memset没有做任何事情,因为您使用零计数参数调用它。

答案 3 :(得分:0)

似乎是一个小伙子,我会解释我的意思

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

这篇文章的目的是,你需要了解隐藏的小细微内容。创建对象数组与unique_ptr的对象数组相同。只有在参数中传递它时才会有所不同。创建unique_ptr的对象指针数组也不是很有用。因此,在大多数情况下,只需要使用两个以下。

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

可能类似以下内容?

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);
相关问题