c - 释放二维数组 - 双重释放或损坏

标签 c arrays double-free

我有以下代码,它简单地动态创建一个矩阵,并根据用户提供给程序的维度用一些随机值填充它:

void initialize(){

    // iteration variables for the loop
    int i,j;

    // allocate some space for the matrix
    Matrix *input = (Matrix *)malloc(sizeof(Matrix));

    if(input == NULL){
        printf("Mem. could not be allocated");
        return;

    }

    // since we have different fcts for randomly filling values into the matrices, 
    // we'll define a fct. pointer
    double (*randomValues)();

    // retrieve & set the dimensions for each dimension
    printf("Please enter the dimensions for input matrix.\n");
    printf("Rows: ");
    scanf("%d", &(input->rows));
    printf("Columns: ");
    scanf("%d", &(input->columns));

    printf("You entered the values: \n");
    printf("Rows: %d\n", input->rows);
    printf("Columns: %d\n", input->columns);

    input->mats = (double **)malloc(input->rows * sizeof(double *));

    if(input->mats == NULL){
        printf("Mem. could not be allocated");
        return;

    }

    // set fct. to simple_randomInput() for the input matrix
    randomValues = simple_randomInput;

    for(i=0; i<input->columns;i++){
        input->mats[i] = (double *)malloc(input->columns * sizeof(double));

        if(input->mats[i] == NULL){
            printf("Mem. could not be allocated");
            return;
        }
    }

    // fill the input matrix randomly
    for(i=0; i<input->rows; i++){
        for(j=0;j<input->columns; j++){
            input->mats[i][j] = (*randomValues)();
        }
    }

    // print those values -> ONLY for testing purposes
    for (i = 0; i<input->rows; i++){
        for (j = 0; j < input->columns; j++){
            printf("%f ", input->mats[i][j]);
        }
        printf("\n");
    }  

    printf("Now we are freeing\n");
    for(i=0;i<input->rows;i++){
        free(input->mats[i]);
    }

    free(input->mats);  
    free(input);
}

前面的代码在我称为“initialize()”的函数中。它在 main() 内部调用。 引用的结构在头文件中:

typedef struct _Matrix{

    int rows;
    int columns;
    double **mats;

}Matrix;

但是我得到了一个未定义的行为。有时,它有效,有时无效。 例如:对于输入 5(行)和 6(列),我得到以下输出:

> Rows: 5 Columns: 6
> 3.000000 6.000000 7.000000 5.000000 3.000000 5.000000 
> 6.000000 2.000000 9.000000 1.000000 2.000000 7.000000 
> 0.000000 9.000000 3.000000 6.000000 0.000000 6.000000 
> 2.000000 6.000000 1.000000 8.000000 7.000000 9.000000 
> 2.000000 0.000000 2.000000 3.000000 7.000000 5.000000  Now we are freeing
> *** Error in `./neural_network': double free or corruption (out): 0x000055faa2dbb880 ***
> ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x7908b)[0x7f5e7dcac08b]
> /lib/x86_64-linux-gnu/libc.so.6(+0x82c3a)[0x7f5e7dcb5c3a] ..... and so
> on ...

注意: simple_randomInput() 是一个函数。它只返回 rand() % 10 的结果。 为了简洁起见,我没有添加它。

我希望有人能提供帮助。我假设我在释放分配的内存空间方面犯了一个错误,但我确实遵循了其他关于分配/释放 2D 数组的教程,它们所做的与我所做的完全相同。然而,我得到了未定义的行为。

最佳答案

您的第一个 for 循环使用 input->columns 而不是 input->rows

像这样改变它:

 for(i=0; i<input->rows;i++){
        input->mats[i] = (double *)malloc(input->columns * sizeof(double));

        if(input->mats[i] == NULL){
            printf("Mem. could not be allocated");
            return;
        }
    }

关于c - 释放二维数组 - 双重释放或损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48586244/

相关文章:

javascript - 将 Javascript 二维数组转换为 ArrayBuffer

c - 带数组的 C 结构的内存布局

java - 在 Java 中转换 'ArrayList<String> to ' String[]'

c++ - 为什么会出现这种 double-free 错误?

c - 不应该出现两次 free()

c - Windows 操作系统中命名信号量的位置是什么

c - 为什么下面的 C 代码中我的 Wordsize 是 64 位?

c - 为什么我的链表打印倒着?

memory-management - 如何检测谁发出了错误的 kfree

c - strcmp 和 __strcmp_sse2_unaligned 之间的区别