释放2D阵列 - 双重免费或损坏

时间:2018-02-02 15:49:10

标签: 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);
}

前面的代码在我调用的函数#34; initialize()&#34;中。它在main()中调用。 引用的结构位于头文件中:

typedef struct _Matrix{

    int rows;
    int columns;
    double **mats;

}Matrix;

但是我得到了一个未定义的行为。有时,它有效,有时则不然。 例如:对于输入5(行)和&amp; 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()是一个fct。它只返回 rand()%10 的结果。 为了简洁起见,我没有添加它。

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

1 个答案:

答案 0 :(得分:2)

您的第一个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;
        }
    }