0

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

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()是一个 fct。它只是返回rand() % 10的结果。为简洁起见,我没有添加它。

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

4

1 回答 1

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;
        }
    }
于 2018-02-02T16:06:50.700 回答