C++ 删除动态分配的二维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30720594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deleting a dynamically allocated 2D array
提问by Alex
So I'm used to memory management in C where free(pointer)
will free up all space pointed to by pointer
. Now I'm confusing myself when attempting to do something simple in C++.
所以我习惯于在 C 中进行内存管理,在那里free(pointer)
将释放pointer
. 现在我在尝试用 C++ 做一些简单的事情时让自己感到困惑。
If I have a 2D array of doubles allocated in a manner similar to this
如果我有一个以类似于此的方式分配的 2D 双打数组
double** atoms = new double*[1000];
for(int i = 0; i < 1000; i++)
atoms[i] = new double[4];
what would be the correct method of freeing the memory on the heap allocated by new?
释放new分配的堆上的内存的正确方法是什么?
My thoughts were originally this (because my brain was thinking in C):
我的想法最初是这样的(因为我的大脑在用 C 语言思考):
for(int i = 0; i < 1000; i++)
delete atoms[i];
delete atoms;
But I had forgotten the existence of the delete[]
operator so I believe the correct method is as follows:
但是我忘记了delete[]
运算符的存在,所以我相信正确的方法如下:
for(int i = 0; i < 1000; i++)
delete[] atoms[i];
delete[] atoms;
Is it important to understand the difference between the delete
and delete[]
operators? Or can I just assume that whenever I allocate an array with ptr = new x[]
I must also deallocate it with delete[] ptr
?
了解delete
和delete[]
运算符之间的区别很重要吗?或者我可以假设每当我分配一个数组时,ptr = new x[]
我也必须用它来释放它delete[] ptr
?
回答by CinchBlue
In reality, an array of pointers pointed to by a pointer is still an array of integral data types or numbers to hold the memory addresses. You should use delete[]
for both.
实际上,一个指针指向的指针数组仍然是一个整数数据类型或数字数组,用于保存内存地址。你应该同时使用delete[]
两者。
Also, yes, a new[]
implies a delete[]
.
另外,是的, anew[]
意味着 a delete[]
。
When you create an array of arrays, you're actually creating an array of numbersthat happen to hold the memory address for another array of numbers. Regardless, they're both arrays of numbers, so delete both with delete[]
.
当您创建一个数组数组时,您实际上是在创建一个数字数组,该数组恰好保存了另一个numbers 数组的内存地址。无论如何,它们都是数字数组,所以用delete[]
.
http://coliru.stacked-crooked.com/a/8a625b672b66f6ce
http://coliru.stacked-crooked.com/a/8a625b672b66f6ce
#include <iostream>
int main() {
//Hey, pointers have a finite size, no matter the indirection level!
std::cout << "sizeof(int*): " << sizeof(int*) << std::endl;
std::cout << "sizeof(int**): " << sizeof(int**) << std::endl;
std::cout << "sizeof(int***): " << sizeof(int***) << std::endl;
//Create an array of pointers that points to more arrays
int** matrix = new int*[5];
for (int i = 0; i < 5; ++i) {
matrix[i] = new int[5];
for (int j = 0; j < 5; ++j) {
matrix[i][j] = i*5 + j;
}
}
//Print out the matrix to verify we have created the matrix
for (int j = 0; j < 5; ++j) {
for (int i = 0; i < 5; ++i) {
std::cout << matrix[j][i] << std::endl;
}
}
//Free each sub-array
for(int i = 0; i < 5; ++i) {
delete[] matrix[i];
}
//Free the array of pointers
delete[] matrix;
return 0;
}