Linux 创建保存已分配数组的 unique_ptr 的正确方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21377360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:55:00  来源:igfitidea点击:

Proper way to create unique_ptr that holds an allocated array

c++linuxgccc++11unique-ptr

提问by lauw

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.

创建一个 unique_ptr 的正确方法是什么,它包含一个在免费存储上分配的数组?Visual Studio 2013 默认支持此功能,但是当我在 Ubuntu 上使用 gcc 4.8.1 版时,会出现内存泄漏和未定义行为。

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:

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)

采纳答案by juanchopanza

Using the T[]specialisation:

使用T[]专业:

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

Note that, in an ideal world, you would not have to explicitly use newto instantiate a unique_ptr, avoiding a potential exception safety pitfall. To this end, C++14 provides you with the std::make_uniquefunction template. See this excellent GOTWfor more details. The syntax is:

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

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

回答by galop1n

Use the array version :

使用阵列版本:

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

Or with c++14, a better form ( VS2013 already have it ):

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

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

回答by TemplateRex

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

最有可能的更好的办法是使用std::vector<unsigned char>替代

#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::vectorcosts a little more because it keeps track of the size and capacity as well, rather than only the beginning of the data.

有一个警告:如果您要大量移动数据,a 会std::vector花费更多,因为它还会跟踪大小和容量,而不仅仅是数据的开头。

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

注意:您memset什么都不做,因为您使用零计数参数调用它。

回答by Raghavendar Reddy

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 的对象数组相同。只有当你在参数中传递它时,它才会有所作为。创建 unique_ptr 的对象指针数组也不是很有用。所以大部分场景下只需要下面两个就可以了。

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

回答by Competent Bit

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

回答by bruin

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