如何正确删除已知第二维大小的二维数组?

3

我的数组:

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

我应该如何正确地删除它?

编辑: 我知道删除的操作,但是我不确定是否应该像这样删除。

delete[] arr;

or like this

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

4
http://en.cppreference.com/w/cpp/container - Biffen
1
可能是What is the array form of 'delete'?的重复问题。 - codebender
1
如果必须使用 new,请考虑使用 auto arr = new int[100][100]; 使类型名称更易读。 - Carl
1
为什么不直接使用std::vector<std::vector<int>>来实现相同的功能,而无需担心删除问题。 - xiaodong
1个回答

4

一个简单的delete[] arr;就可以了。

无论你释放new int[100][100][100][100]还是new int[100],都使用delete[] x;。如果是单个对象,则使用delete x;


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接