I'm creating a 2D Array in C; am I freeing it correctly?
我用C创建一个二维数组;我是否正确地释放了它?
// create
int n = 3;
int (*X)[n] = malloc(sizeof(int[n][n]));
// set to 0
for(int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
X[i][j] = 0;
}
}
// is this all I need?
free(X);
2 个解决方案
#1
3
You must call free
once for each malloc
. Your code has one malloc
and one free
(with the same address) so it is correct.
你必须为每一个malloc打一次免费电话。您的代码有一个malloc和一个free(具有相同的地址),所以它是正确的。
#2
0
For every malloc()
, there must be a matching free()
. So if you call malloc()
in a loop, there must be a similar loop later on that calls free()
just as many times. Your code has one malloc()
and one corresponding free()
, so yes, you have free
d everything you need to.
对于每个malloc(),必须有一个匹配的free()。因此,如果您在循环中调用malloc(),那么在以后调用free()的时候一定会有一个类似的循环。您的代码有一个malloc()和一个相应的free(),因此,是的,您已经释放了需要的所有内容。
The most common way I've seen to allocate a two-dimensional array is to do something like:
我所见过的分配二维数组的最常见方法是:
int **arr = malloc(sizeof (int *) * num_rows);
for (i = 0; i < num_rows; ++i)
arr[i] = malloc(sizeof (int) * num_cols);
/*
* Using `sizeof *arr` and `sizeof **arr`
* respectively would have been more safe,
* but for the purpose of this example, I think
* using `int *` and `int` is simpler to
* understand.
*/
then later to free:
然后*:
for (i = 0; i < num_rows; ++i)
free(arr[i]);
free(arr);
This makes the outer dimension hold num_row
pointers to pointers to int (each of the num_row
pointers points to the starting address of the 'column'). So each element in the outer dimension points to a row, and in each row there are num_cols
elements in the inner dimension (the 'columns'), which is just a group of num_cols
integers. Does this make sense to you? So you have to allocate num_rows
integer pointers, and each one of these points to the first 'column' of that row--for each row you have to allocate space for num_cols
integers, so you can see the loop will make a total of num_rows * num cols
integers, and they way they are allocated allows you to use array indexing notation (two-dimensional in this case--each element in the outer dimension points to the start of a 'row', which contains a pointer to the start of a 'column', hence the double pointer) to access the elements. I know this is probably confusing, that is why I tried to describe it so many different times/ways, but please just ask any questions, especially about what you don't understand in particular, and I will be more than happy to work with you in helping understand it.
这使得外部维度保存指向int的num_row指针(每个num_row指针指向'column'的起始地址)。因此,外部维度中的每个元素指向一行,而在每一行中,内部维度(“列”)中都有num_cols元素,这是一组num_cols整数。这对你有意义吗?所以你必须分配num_rows整数指针,和每一个点到第一列的这一行,每一行需要分配空间num_cols整数,所以你可以看到循环将总共num_rows * num整数关口,他们的方式分配允许您使用数组索引符号(二维在这种情况下,每个元素在外层维度指向“行”的开始,它包含一个指针指向“列”的开始,因此有双指针)来访问元素。我知道这可能会让人困惑,这就是为什么我试着用不同的方式描述它,但是请提出任何问题,特别是关于你不理解的东西,我将非常高兴与你一起帮助你理解它。
I'm not saying you have to create your 2-D array this way; your way works fine too, I just wanted to show you this way since you are likely to run into it since it's very common.
我不是说你必须用这种方法创建二维数组;你的方法也很有效,我只是想给你演示一下,因为你很可能会遇到它,因为它很常见。
Also, look into Valgrind. You can use that tool to see if you have forgotten any free()
s / have an unmatched malloc()
, and lets you know if there is any allocated memory unreachable/leaked (maybe the pointer was changed before the call to free()
so the call doesn't properly free the memory and you get a leak).
另外,看着Valgrind。您可以使用这个工具来看看你是否忘记了任何*()s /有一个无与伦比的malloc(),并让你知道如果有任何分配的内存访问/泄露(可能调用前的指针改变了*()的调用并不妥善释放内存,你得到一个泄漏)。
#1
3
You must call free
once for each malloc
. Your code has one malloc
and one free
(with the same address) so it is correct.
你必须为每一个malloc打一次免费电话。您的代码有一个malloc和一个free(具有相同的地址),所以它是正确的。
#2
0
For every malloc()
, there must be a matching free()
. So if you call malloc()
in a loop, there must be a similar loop later on that calls free()
just as many times. Your code has one malloc()
and one corresponding free()
, so yes, you have free
d everything you need to.
对于每个malloc(),必须有一个匹配的free()。因此,如果您在循环中调用malloc(),那么在以后调用free()的时候一定会有一个类似的循环。您的代码有一个malloc()和一个相应的free(),因此,是的,您已经释放了需要的所有内容。
The most common way I've seen to allocate a two-dimensional array is to do something like:
我所见过的分配二维数组的最常见方法是:
int **arr = malloc(sizeof (int *) * num_rows);
for (i = 0; i < num_rows; ++i)
arr[i] = malloc(sizeof (int) * num_cols);
/*
* Using `sizeof *arr` and `sizeof **arr`
* respectively would have been more safe,
* but for the purpose of this example, I think
* using `int *` and `int` is simpler to
* understand.
*/
then later to free:
然后*:
for (i = 0; i < num_rows; ++i)
free(arr[i]);
free(arr);
This makes the outer dimension hold num_row
pointers to pointers to int (each of the num_row
pointers points to the starting address of the 'column'). So each element in the outer dimension points to a row, and in each row there are num_cols
elements in the inner dimension (the 'columns'), which is just a group of num_cols
integers. Does this make sense to you? So you have to allocate num_rows
integer pointers, and each one of these points to the first 'column' of that row--for each row you have to allocate space for num_cols
integers, so you can see the loop will make a total of num_rows * num cols
integers, and they way they are allocated allows you to use array indexing notation (two-dimensional in this case--each element in the outer dimension points to the start of a 'row', which contains a pointer to the start of a 'column', hence the double pointer) to access the elements. I know this is probably confusing, that is why I tried to describe it so many different times/ways, but please just ask any questions, especially about what you don't understand in particular, and I will be more than happy to work with you in helping understand it.
这使得外部维度保存指向int的num_row指针(每个num_row指针指向'column'的起始地址)。因此,外部维度中的每个元素指向一行,而在每一行中,内部维度(“列”)中都有num_cols元素,这是一组num_cols整数。这对你有意义吗?所以你必须分配num_rows整数指针,和每一个点到第一列的这一行,每一行需要分配空间num_cols整数,所以你可以看到循环将总共num_rows * num整数关口,他们的方式分配允许您使用数组索引符号(二维在这种情况下,每个元素在外层维度指向“行”的开始,它包含一个指针指向“列”的开始,因此有双指针)来访问元素。我知道这可能会让人困惑,这就是为什么我试着用不同的方式描述它,但是请提出任何问题,特别是关于你不理解的东西,我将非常高兴与你一起帮助你理解它。
I'm not saying you have to create your 2-D array this way; your way works fine too, I just wanted to show you this way since you are likely to run into it since it's very common.
我不是说你必须用这种方法创建二维数组;你的方法也很有效,我只是想给你演示一下,因为你很可能会遇到它,因为它很常见。
Also, look into Valgrind. You can use that tool to see if you have forgotten any free()
s / have an unmatched malloc()
, and lets you know if there is any allocated memory unreachable/leaked (maybe the pointer was changed before the call to free()
so the call doesn't properly free the memory and you get a leak).
另外,看着Valgrind。您可以使用这个工具来看看你是否忘记了任何*()s /有一个无与伦比的malloc(),并让你知道如果有任何分配的内存访问/泄露(可能调用前的指针改变了*()的调用并不妥善释放内存,你得到一个泄漏)。