[英]memory deallocation with delete [] for 2d array with one dimension constant
I allocated a 2D array using new like this 我像这样使用new分配了2D数组
int (*arr)[2] = new int[x][2];
x is calculated at runtime. x在运行时计算。
How should I deallocate this memory? 我应该如何释放该内存? I tried 我试过了
delete [] *arr[0];
delete [] *arr[1];
However, valgrind says that their were 13 allocs and 14 frees. 但是,valgrind说他们有13个分配器和14个释放器。
You use one new[]
, so you should use one delete[]
: 您使用一个new[]
,所以您应该使用一个delete[]
:
delete[] arr;
The delete[]
form applies to any new[]
; delete[]
表格适用于任何new[]
; there aren't separate cases for new T[x]
as for new T[x][2]
or anything. 有不单独箱子new T[x]
为new T[x][2]
或任何东西。 An expression like new T[x][2]
is actually a single array new
where the array element has type "array of 2 T's". 像new T[x][2]
这样的表达式实际上是一个new
数组,其中array元素的类型为“ 2个T的数组”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.