繁体   English   中英

释放2D阵列-两次释放或损坏

[英]Freeing a 2D Array - double free or corruption

我有以下代码,它们可以简单地动态创建一个矩阵,并根据用户赋予程序的尺寸用一些随机值填充它:

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

暂无
暂无

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

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