Newbie trying to learn C here. I'm trying to write a program that allocates memory for a matrix, scans it, prints it and finally frees the memory. It first scans the number of rows and columns and then allocates memory for the matrix. Then it prints the matrix and finally frees the memory. (At least this is what I intend it to do). It seemed like it was working and the output is as I expect it to be, but when I run it with valgrind, it says "Invalid read of size 8 ... in 'free matrix'". I can't figure out why this is the case. Am I doing something wrong when I free the allocated memory? (Or elsewhere?). Any help is much appreciated. I have enclosed the code.
#include<stdio.h>
#include<stdlib.h>
double** create_matrix(int, int);
void scan_matrix(double** ,int,int);
void free_matrix(double**,int);
void print_matrix(double** ,int,int);
int main(){
int m,n;
scanf("%d %d",&m,&n);
double** A = create_matrix(m,n);
scan_matrix(A,m,n);
print_matrix(A,m,n);
free_matrix(A,m);
return 0;
}
double** create_matrix(int m, int n){
double** matrix;
//First allocate memory for storing all the rows of the matrix:
matrix = (double**)malloc(m*sizeof(double*));
//Check that memory could be allocated:
if(matrix==NULL){
printf("Memory could not be allocated for initial pointer\n");
}
//Then allocate memory for all the elements on each of the rows:
for(int i= 0; i<m;i++){
// *(matrix+i) = (double*) malloc(n*sizeof(double));
*(matrix+i) = (double*) malloc(n*sizeof(double));
if(*(matrix+i)==NULL){
printf("Memory could not be allocated for row %d\n",i);
}
}
return matrix;
}
/*
*matrix is a pointer to a pointer. m is the number of rows.
*/
void free_matrix(double** matrix,int m){
//First free memory allocated for all the rows.
for(int i= 0; i<m;i++){
free(matrix+i);
}
//Then free the initial array pointer:
free(matrix);
}
//Function for scanning matrix:
void scan_matrix(double** array,int rows, int cols){
for(int i = 0; i<rows;i++){
for(int j = 0; j<cols;j++){
scanf("%lf",*(array+i)+j);
}
}
}
void print_matrix(double** matrix,int m, int n){
for(int i= 0; i<m;i++){
for(int j= 0; j<n;j++){
printf("%10.2lf ",matrix[i][j]);
if(j==n-1)
printf("\n");
}
}
}
As kaylum said you need to make the free function as: free(matrix[i])
The reason is that when using free (in a matrix that is allocated in a way as you did) we need to pass the specific address of the rows allocated memory. When you sent matrix + i
you referred an address of the entire matrix (e.g. when 'i' equals zero) and not one of the rows.