释放C中指针指针的动态内存
releasing dynamic memory of pointer of pointers in C
我在尝试释放动态数组的内存时出错。
void Ex()
{
int **Matrix = dyn_matrix_allocation(rows, cols);
.
.
.
free_My_Dyn_Mem(Matrix, rows);
}
void free_My_Dyn_Mem(int **to_Release, int size)
{
int i;
for (i = 0; i < size; i++)
free(to_Release[i]);
//free(to_Release); - If I do this, the program crash.
}
void **dyn_matrix_allocation(int rows, int cols)
{
void **matrix = (void**)calloc (sizeof(int*), cols);
assert(matrix); /*calloc worked*/
int i;
for (i = 0; i < rows; i++)
{
matrix[i] = (int*)calloc(sizeof(int), rows);
assert(matrix[i]); /*calloc worked*/
}
return matrix;
}
释放数组本身后,我试图释放指针的指针 (**matrix)
然后我得到一个错误。调试器显示没有什么特别的。有什么想法吗?
由于您的 2D 寻址定义不明确,您在分配动态矩阵时犯了几个错误。
分配中的第一个错误,在这里您选择创建依赖于分配 cols
指针数组的矩阵列 ti int
:
void **matrix = (void**)calloc (sizeof(int*), cols);
现在您应该为每列分配一个 rows
个整数数组,但是您分配 rows
个整数数组:
for (i = 0; i < rows; i++) //Should be for (i = 0; i < cols; i++)
{
matrix[i] = (int*)calloc(sizeof(int), rows);
assert(matrix[i]); /*calloc worked*/
}
到目前为止,一些编译器、lint 甚至好的调试器应该已经告诉您您在哪里超出了范围。
但是当您仍然使用错误的寻址释放内存时会触发异常。
void Ex()
{
int **Matrix = dyn_matrix_allocation(rows, cols);
.
.
.
free_My_Dyn_Mem(Matrix, rows); //But you allocated cols pointers...
}
您应该传递大小为 cols
个成员的整数指针数组,而不是 rows
。
现在你释放你分配的越界:
for (i = 0; i < size; i++)
free(to_Release[i]);
调试器应该抱怨多了吧!
然后你释放一个现在损坏的内存...
free(to_Release);
您的代码应该是:
#include <stdlib.h>
#include <assert.h>
void free_My_Dyn_Mem(int **to_Release, int size)
{
int i;
for (i = 0; i < size; i++)
free(to_Release[i]);
free(to_Release); //- If I do this, the program crash.
}
int **dyn_matrix_allocation(int rows, int cols)
{
int **matrix = calloc(sizeof(int *), cols);
assert(matrix); /*calloc worked */
int i;
//for (i = 0; i < rows; i++)
for (i = 0; i < cols; i++)
{
matrix[i] = calloc(sizeof(int), rows);
assert(matrix[i]); /*calloc worked */
}
return matrix;
}
void Ex(void)
{
int rows = 100;
int cols = 20;
int **Matrix = dyn_matrix_allocation(rows, cols);
//.
//.
//.
//free_My_Dyn_Mem(Matrix, rows);
free_My_Dyn_Mem(Matrix, cols);
}
请记住,您选择了列有序矩阵。
P.S。我忘了补充一点,断言通常用于开发检查,可以通过定义符号 NDEBUG
来删除它们。当您需要永久控制时,例如分配错误 return,您应该使用标准 if (condition) ErrorHandling(...);
.
我在尝试释放动态数组的内存时出错。
void Ex()
{
int **Matrix = dyn_matrix_allocation(rows, cols);
.
.
.
free_My_Dyn_Mem(Matrix, rows);
}
void free_My_Dyn_Mem(int **to_Release, int size)
{
int i;
for (i = 0; i < size; i++)
free(to_Release[i]);
//free(to_Release); - If I do this, the program crash.
}
void **dyn_matrix_allocation(int rows, int cols)
{
void **matrix = (void**)calloc (sizeof(int*), cols);
assert(matrix); /*calloc worked*/
int i;
for (i = 0; i < rows; i++)
{
matrix[i] = (int*)calloc(sizeof(int), rows);
assert(matrix[i]); /*calloc worked*/
}
return matrix;
}
释放数组本身后,我试图释放指针的指针 (**matrix)
然后我得到一个错误。调试器显示没有什么特别的。有什么想法吗?
由于您的 2D 寻址定义不明确,您在分配动态矩阵时犯了几个错误。
分配中的第一个错误,在这里您选择创建依赖于分配 cols
指针数组的矩阵列 ti int
:
void **matrix = (void**)calloc (sizeof(int*), cols);
现在您应该为每列分配一个 rows
个整数数组,但是您分配 rows
个整数数组:
for (i = 0; i < rows; i++) //Should be for (i = 0; i < cols; i++)
{
matrix[i] = (int*)calloc(sizeof(int), rows);
assert(matrix[i]); /*calloc worked*/
}
到目前为止,一些编译器、lint 甚至好的调试器应该已经告诉您您在哪里超出了范围。
但是当您仍然使用错误的寻址释放内存时会触发异常。
void Ex()
{
int **Matrix = dyn_matrix_allocation(rows, cols);
.
.
.
free_My_Dyn_Mem(Matrix, rows); //But you allocated cols pointers...
}
您应该传递大小为 cols
个成员的整数指针数组,而不是 rows
。
现在你释放你分配的越界:
for (i = 0; i < size; i++)
free(to_Release[i]);
调试器应该抱怨多了吧!
然后你释放一个现在损坏的内存...
free(to_Release);
您的代码应该是:
#include <stdlib.h>
#include <assert.h>
void free_My_Dyn_Mem(int **to_Release, int size)
{
int i;
for (i = 0; i < size; i++)
free(to_Release[i]);
free(to_Release); //- If I do this, the program crash.
}
int **dyn_matrix_allocation(int rows, int cols)
{
int **matrix = calloc(sizeof(int *), cols);
assert(matrix); /*calloc worked */
int i;
//for (i = 0; i < rows; i++)
for (i = 0; i < cols; i++)
{
matrix[i] = calloc(sizeof(int), rows);
assert(matrix[i]); /*calloc worked */
}
return matrix;
}
void Ex(void)
{
int rows = 100;
int cols = 20;
int **Matrix = dyn_matrix_allocation(rows, cols);
//.
//.
//.
//free_My_Dyn_Mem(Matrix, rows);
free_My_Dyn_Mem(Matrix, cols);
}
请记住,您选择了列有序矩阵。
P.S。我忘了补充一点,断言通常用于开发检查,可以通过定义符号 NDEBUG
来删除它们。当您需要永久控制时,例如分配错误 return,您应该使用标准 if (condition) ErrorHandling(...);
.