简体   繁体   English

在C中释放指针的动态内存

[英]releasing dynamic memory of pointer of pointers in C

I have an error while trying to release memory of dynamic array. 尝试释放动态数组的内存时出现错误。

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;
}

after releasing the array itself, I'm trying to release the pointer of pointers (**matrix) then I get an error. 在释放数组本身之后,我试图释放指针的指针(**matrix)然后出现错误。 debugger shows nothing special. 调试器显示没什么特别的。 any ideas why? 有什么想法吗?

You made a couple of errors in allocating your dynamic matrix due to unclear definition of your 2D addressing. 由于2D寻址的定义不清楚,您在分配动态矩阵时遇到了一些错误。

First error in allocation, here you choose to create your matrix column dependent allocating an array of cols pointers ti int : 分配中的第一个错误,在这里您选择创建与矩阵列相关的分配cols指针ti int数组:

void **matrix = (void**)calloc (sizeof(int*), cols);

Now you should allocate an array of rows integers per each column , but you assign rows arrays of integers: 现在,你应该分配的数组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*/
}

Up to now some compilers, or lint or even good debuggers should have told you that you where outside bounds. 到目前为止,一些编译器,lint或什至是好的调试器都应该告诉您您超出了范围。

But the exception triggers when you free the memory still using the wrong addressing. 但是,当您仍然使用错误的地址释放内存时,会触发异常。

void Ex()
{
 int **Matrix = dyn_matrix_allocation(rows, cols);
.
.
.
    free_My_Dyn_Mem(Matrix, rows);  //But you allocated cols pointers...
}

You should pass the array of integer pointers that has a size of cols members, not rows . 您应该传递整数指针数组,该数组的大小为cols成员,而不是rows

Now you release what you allocated out of bounds: 现在,释放超出范围的分配:

for (i = 0; i < size; i++)
    free(to_Release[i]);

The debugger should have complained a lot! 调试器应该抱怨很多!

Then you release a now corrupted memory... 然后释放一个损坏的内存...

free(to_Release);

Your code should be: 您的代码应为:

#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);
}

Remember that you chosen a columns ordered matrix. 记住,您选择了列排序矩阵。

PS I forget to add that asserts are normally used for development checks and they can be removed defining the symbol NDEBUG . PS我忘了补充一点,断言通常用于开发检查,可以将它们删除,并定义符号NDEBUG When you need a permanent control, like an error on allocation return, you should use standard if (condition) ErrorHandling(...); 当需要永久控制时,例如分配返回错误,应使用标准if (condition) ErrorHandling(...); .

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

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