Working on some C code working with Matrices. I have created a Matrix struct to make it easier to create many instances of it quickly and efficiently but I am not sure why my code is leaking memory. Currently I have the following code:
typedef struct
{
size_t rows;
size_t cols;
double ** value;
}
*Matrix;
Matrix matCreate( size_t rows, size_t cols )
{
if(rows<=0 || cols<=0)
{
return NULL;
}
Matrix mat = (Matrix) malloc(sizeof(Matrix));
mat->rows = rows;
mat->cols = cols;
mat->value = (double **) malloc(rows*sizeof(double*));
for (int i=0; i< rows;i++)
{
mat->value[i] = (double *) malloc(cols * sizeof(double));
for( int j=0; j< cols;j++)
{
mat->value[i][j] = 0;
if(rows == cols && i==j )
{
mat->value[i][j] = 1;
}
}
}
return mat;
}
And I am getting the following memory leaks after running valgrind. When running the code it compiles completely without error and still outputs the right output.
==23609== Invalid write of size 8
==23609== at 0x400800: matCreate
==23609== by 0x4010E2: main
==23609== Address 0x5203048 is 0 bytes after a block of size 8 alloc'd
==23609== at 0x4C2DB8F: malloc
==23609== by 0x4007E8: matCreate
==23609== by 0x4010E2: main
==23609==
==23609== Invalid write of size 8
==23609== at 0x40081B: matCreate
==23609== by 0x4010E2: main
==23609== Address 0x5203050 is 8 bytes after a block of size 8 alloc'd
==23609== at 0x4C2DB8F:
==23609== by 0x4007E8: matCreate
==23609== by 0x4010E2: main
==23609==
==23609== Invalid read of size 8
==23609== at 0x40082F: matCreate
==23609== by 0x4010E2: main
==23609== Address 0x5203050 is 8 bytes after a block of size 8 alloc'd
==23609== at 0x4C2DB8F: malloc
==23609== by 0x4007E8: matCreate
==23609== by 0x4010E2: main