int matrix with pointers in C - memory allocation confusion

后端 未结 7 1329
春和景丽
春和景丽 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:30

    You need to free each row individually:

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

提交回复
热议问题