C++ 删除指向指针的指针(作为数组数组)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4193982/
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
Delete a pointer to pointer (as array of arrays)
提问by yelo3
I have this in my code:
我的代码中有这个:
double** desc = new double* [size_out];
for (int i = 0; i < size_out; i++)
desc[i] = new double [size_in];
How do I delete this desc
?
我如何删除这个desc
?
Should I do:
我应该这样做:
delete [] desc;
or
或者
for (int i=0; i<size_out; i++)
delete [] desc[i];
delete [] desc;
or
或者
for (int i=0; i<size_out; i++)
delete [] desc[i];
delete desc;
?
?
采纳答案by ?imon Tóth
Simple rules to follow:
要遵循的简单规则:
- for each allocation, there has to be a deallocation (ex1 is therefore wrong)
- what was allocated using
new
should be freed usingdelete
, usingnew[]
should be deallocated usingdelete[]
and usingmalloc
should be deallocated usingfree
(ex3 is therefore wrong)
- 对于每个分配,必须有一个解除分配(因此 ex1 是错误的)
- 分配 using 的内容
new
应该释放 usingdelete
, usingnew[]
应该释放 usingdelete[]
并且 usingmalloc
应该释放 usingfree
(因此 ex3 是错误的)
Conclusion, ex2 is OK.
结论,ex2没问题。
回答by CB Bailey
Your code shouldn't compile. The type of an array new expression is a pointer to the type of array element being created (the value is a pointer to the first element of the allocated array).
你的代码不应该编译。数组 new 表达式的类型是指向正在创建的数组元素类型的指针(值是指向已分配数组的第一个元素的指针)。
So the type of new double**[size_out]
is double ***
.
所以类型new double**[size_out]
是double ***
。
Whenever you use the array form of new, you must use the array form of delete even if you only allocate an array of size one.
每当使用 new 的数组形式时,即使只分配大小为 1 的数组,也必须使用 delete 的数组形式。
double*** desc = new double**[size_out];
for (int i=0; i<size_out; i++)
desc[i] = new double*[size_in];
for (int i=0; i<size_out; i++)
delete[] desc[i];
delete[] desc;
Note that you still haven't allocated any double
, just pointers.
请注意,您还没有分配任何double
,只是指针。
Did you really want this instead?
你真的想要这个吗?
double** desc = new double*[size_out];
for (int i=0; i<size_out; i++)
desc[i] = new double[size_in];
for (int i=0; i<size_out; i++)
delete[] desc[i];
delete[] desc;
回答by Konrad Rudolph
Your deletion should mirror your allocation.
您的删除应该反映您的分配。
Since you used new []
to allocate the outer array, and new []
(in a loop) to allocate the inner arrays, do likewise for deletion. That is: your second solution is correct; delete []
the inner arrays in a loop, and finally the outer array via delete []
also.
由于您曾经new []
分配外部数组,并且new []
(在循环中)分配内部数组,因此删除也是如此。那就是:你的第二个解决方案是正确的;delete []
循环中的内部数组,最后也是通过外部数组delete []
。
That said, a (much, much) better solution in C++ would be to use a nested std::vector
:
也就是说,在 C++ 中(很多,很多)更好的解决方案是使用嵌套的std::vector
:
// Declaration and initialization:
vector<vector<double> > desc(size_out, vector<double>(size_in));
// No deletion!
回答by icecrime
Solution 2is the right one : each cell points to a dynamically allocated array that should be deleted using delete[]
. Finally,
the desc
array itself should be deleted using delete[]
.
解决方案 2是正确的:每个单元格都指向一个动态分配的数组,应该使用delete[]
. 最后,desc
应该使用 删除数组本身delete[]
。
Bonus solution 4: avoid using arrays and switch to std::vector<std::vector<double> >
.
奖励解决方案 4:避免使用数组并切换到std::vector<std::vector<double> >
.
回答by Stephane Rolland
I would do
我会做
for (int i=0; i<size_out; i++)
delete [] desc[i];
delete [] desc;
for each array allocated with new []
, you have a corresponding delete []
.
对于每个分配有 的数组new []
,您都有一个对应的delete []
.
And as Rupdolph says: stop using C-arrays, and start using std::vector
. You will have less bugs (hundred times less bugs).
正如 Rupdolph 所说:停止使用 C 数组,并开始使用std::vector
. 您将拥有更少的错误(错误减少数百倍)。