![](/img/trans.png)
[英]Is it dumb to make a dynamic 2d array where the second dimension is a constant?
[英]memory deallocation with delete [] for 2d array with one dimension constant
我像這樣使用new分配了2D數組
int (*arr)[2] = new int[x][2];
x在運行時計算。
我應該如何釋放該內存? 我試過了
delete [] *arr[0];
delete [] *arr[1];
但是,valgrind說他們有13個分配器和14個釋放器。
您使用一個new[]
,所以您應該使用一個delete[]
:
delete[] arr;
delete[]
表格適用於任何new[]
; 有不單獨箱子new T[x]
為new T[x][2]
或任何東西。 像new T[x][2]
這樣的表達式實際上是一個new
數組,其中array元素的類型為“ 2個T的數組”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.