ebeninki
ebeninki

Reputation: 989

Freeing a 2D Array - double free or corruption

I have the following code which simply creates a matrix dynamically and fills it with some random values based on the dimension the user gives to the program:

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

The previous code is within a function that I called "initialize()". It is called inside main(). The structure that is referenced is in the header file:

typedef struct _Matrix{

    int rows;
    int columns;
    double **mats;

}Matrix;

But I get an undefined behavior. Sometimes, it works and sometimes it is not. For example: For the inputs 5 (rows) & 6 (columns), I get the following output:

> 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 ...

NOTE: simple_randomInput() is a fct. which simply returns result of rand() % 10. For the sake of brevity, I did not add it.

I hope someone can help. I assume that I made a mistake in terms of freeing the allocated memory spaces but I did follow other tutorials regarding allocating/freeing 2D arrays & and they did exactly the same what I did. Nevertheless, I get the undefined behavior.

Upvotes: 0

Views: 721

Answers (1)

nabil.douss
nabil.douss

Reputation: 640

Your first for loop is using input->columns instead of input->rows

change it like this :

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

Upvotes: 2

Related Questions