I allocated a 2D array using new like this
int (*arr)[2] = new int[x][2];
x is calculated at runtime.
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.
You use one new[]
, so you should use one delete[]
:
delete[] arr;
The delete[]
form applies to any new[]
; there aren't separate cases for new T[x]
as for new T[x][2]
or anything. An expression like new T[x][2]
is actually a single array new
where the array element has type "array of 2 T's".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.