Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我能';t使列表具有唯一性<;int>;阵列_C++_Arrays_List_Stl_Unique Ptr - Fatal编程技术网

C++ 我能';t使列表具有唯一性<;int>;阵列

C++ 我能';t使列表具有唯一性<;int>;阵列,c++,arrays,list,stl,unique-ptr,C++,Arrays,List,Stl,Unique Ptr,这个简单的代码可以编译 unique_ptr<list<int>> map1(new list<int>[10]); unique_ptr map1(新列表[10]); 但它在运行时会导致seg故障 malloc:*对象0x7fe02a4032e8的错误:未分配要释放的指针 *在malloc\u error\u break中设置断点以进行调试 但是,此代码成功运行 unique_ptr<int> map2(new int[10]); uniq

这个简单的代码可以编译

unique_ptr<list<int>> map1(new list<int>[10]);
unique_ptr map1(新列表[10]);
但它在运行时会导致seg故障

malloc:*对象0x7fe02a4032e8的错误:未分配要释放的指针 *在malloc\u error\u break中设置断点以进行调试

但是,此代码成功运行

unique_ptr<int> map2(new int[10]);
unique_ptr map2(新整数[10]);
为什么不能使用unique_ptr创建列表数组


提前感谢。

您需要为动态分配的阵列使用唯一的\u ptr版本:

std::unique_ptr<std::list<int>[]> map1(new std::list<int>[10]);
                              ^^ ~~~~~ !
std::unique_ptr map1(新std::list[10]);
^^ ~~~~~ !
请看这里:

模板<
T类,
类删除器
>类唯一性\u ptr;
您还可以使用(如注释中所建议的)来获得编译错误,而不是UB,因为它的接口可以防止编译错误:

std::unique_ptr<std::list<int>[]> up1 = std::make_unique<std::list<int>[]>(10);
auto up2 = std::make_unique<std::list<int>[]>(10);
std::unique_ptr up1=std::make_unique(10);
自动up2=标准::使_唯一(10);
[编辑]

但是,此代码成功运行

unique_ptr<int> map2(new int[10]);
唯一的ptr map2(新的整数[10])


在上述代码中,其行为仍未定义,可能会工作或导致seg故障。如上所述的unique_ptr接受任何指针,但在析构函数期间将始终调用
delete
。在指向动态分配数组的指针上调用
delete
,是UB。这就是为什么您需要调用
delete[]
,这就是动态分配阵列的
unique\u ptr

您需要为动态分配阵列使用unique\u ptr版本:

std::unique_ptr<std::list<int>[]> map1(new std::list<int>[10]);
                              ^^ ~~~~~ !
std::unique_ptr map1(新std::list[10]);
^^ ~~~~~ !
请看这里:

模板<
T类,
类删除器
>类唯一性\u ptr;
您还可以使用(如注释中所建议的)来获得编译错误,而不是UB,因为它的接口可以防止编译错误:

std::unique_ptr<std::list<int>[]> up1 = std::make_unique<std::list<int>[]>(10);
auto up2 = std::make_unique<std::list<int>[]>(10);
std::unique_ptr up1=std::make_unique(10);
自动up2=标准::使_唯一(10);
[编辑]

但是,此代码成功运行

unique_ptr<int> map2(new int[10]);
唯一的ptr map2(新的整数[10])

在上述代码中,其行为仍未定义,可能会工作或导致seg故障。如上所述的unique_ptr接受任何指针,但在析构函数期间将始终调用
delete
。在指向动态分配数组的指针上调用
delete
,是UB。这就是为什么您需要调用
delete[]
,这就是动态分配阵列的
唯一性。\u ptr

您需要的是:

std::unique_ptr<std::list<int>[]> map1(new std::list<int>[10]);
std::unique_ptr map1(新std::list[10]);
您需要的是:

std::unique_ptr<std::list<int>[]> map1(new std::list<int>[10]);
std::unique_ptr map1(新std::list[10]);
A
A列表
和A
list[10]
是两种不同的类型。当你有一个

unique_ptr<list<int>>
需要使用
delete[]
而不是
delete
来删除它。这对我来说是一样的

unique_ptr<int> map2(new int[10]);
unique_ptr map2(新整数[10]);
为什么第一个版本失败而第二个版本没有失败

对已
new[]
ed的内容调用
delete
是未定义的行为

因此,如果要在构造函数中使用
new[]
,则需要将
type[]
用于
唯一的类型\u ptr

A
列表和
列表[10]
是两种不同的类型。当你有一个

unique_ptr<list<int>>
需要使用
delete[]
而不是
delete
来删除它。这对我来说是一样的

unique_ptr<int> map2(new int[10]);
unique_ptr map2(新整数[10]);
为什么第一个版本失败而第二个版本没有失败

对已
new[]
ed的内容调用
delete
是未定义的行为


因此,如果要在构造函数中使用
new[]
,则需要对
唯一的类型使用
type[]
。\u ptr

这两段代码都有未定义的行为,并且都已损坏。请参阅
列表和
列表[10]
是两种不同的类型,而不是
std::vector
?这两种代码都有未定义的行为,并且都被破坏。参考
列表和
列表[10]
是两种不同的类型,而不是
std::vector
?这就是为什么人们应该使用
std::make_unique
,这就是为什么我们应该使用
std::make_unique
,使类型不可能出错。