int matrix with pointers in C - memory allocation confusion

后端 未结 7 1315
春和景丽
春和景丽 2020-12-18 05:01

I\'m having some issues with producing an int matrix without creating memory leaks. I want to be able to make a given (global) matrix into any size dynamically via read_matr

相关标签:
7条回答
  • 2020-12-18 05:45

    You only freed the first row (or column) of first_matrix. Write another function like this:

    void free_matrix(int **matrix, int rows)
    {
        int i;
        for(i=0; i<rows; i++)
        {
            free(matrix[i]);
        }
        free(matrix);
    }
    

    You might want to make the matrix into a struct to store it's row and column count.

    0 讨论(0)
提交回复
热议问题