简体   繁体   English

如何正确删除已知第二个维度的二维数组?

[英]How to correctly delete 2d array with known second dimension size?

My array: 我的阵列:

int (*arr)[100] = new int[100][100];

How should I correctly delete this? 我应该如何正确删除它?

edit: I know how delete works, but i am not sure if I should delete it like this 编辑:我知道删除是如何工作的,但我不确定是否应该删除它

delete[] arr;

or like this 或者像这样

for(int i = 0; i < 100; i++) {
    delete[] arr[i];
}
delete[] arr;

A simple delete[] arr; 一个简单的delete[] arr; should do just fine. 应该做得很好。

Whether you are freeing new int[100][100][100][100] or new int[100] you use delete[] x; 无论你是在释放new int[100][100][100][100]还是new int[100]你都使用delete[] x; . If it's a single object, you use delete x; 如果它是单个对象,则使用delete x; .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM