Hey guys I got an assignment due tomorrow of multiplying two matrices. I got everything done but when I free the second matrix then I get this error:
Code:
*** glibc detected *** ./a.out: double free or corruption (out): 0x0000000001f5e2f0 ***
======= Backtrace: =========
/lib/libc.so.6[0x7fa69d033a58]
/lib/libc.so.6(cfree+0x76)[0x7fa69d0360a6]
./a.out[0x400aad]
/lib/libc.so.6(__libc_start_main+0xe6)[0x7fa69cfd8466]
./a.out[0x400659]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:05 3498047 /home/craig/Desktop/Cis308/a.out
00601000-00602000 r--p 00001000 08:05 3498047 /home/craig/Desktop/Cis308/a.out
00602000-00603000 rw-p 00002000 08:05 3498047 /home/craig/Desktop/Cis308/a.out
01f5e000-01f7f000 rw-p 01f5e000 00:00 0 [heap]
7fa698000000-7fa698021000 rw-p 7fa698000000 00:00 0
7fa698021000-7fa69c000000 ---p 7fa698021000 00:00 0
7fa69cda2000-7fa69cdb8000 r-xp 00000000 08:05 647270 /lib/libgcc_s.so.1
7fa69cdb8000-7fa69cfb8000 ---p 00016000 08:05 647270 /lib/libgcc_s.so.1
7fa69cfb8000-7fa69cfb9000 r--p 00016000 08:05 647270 /lib/libgcc_s.so.1
7fa69cfb9000-7fa69cfba000 rw-p 00017000 08:05 647270 /lib/libgcc_s.so.1
7fa69cfba000-7fa69d123000 r-xp 00000000 08:05 647470 /lib/libc-2.8.90.so
7fa69d123000-7fa69d322000 ---p 00169000 08:05 647470 /lib/libc-2.8.90.so
7fa69d322000-7fa69d326000 r--p 00168000 08:05 647470 /lib/libc-2.8.90.so
7fa69d326000-7fa69d327000 rw-p 0016c000 08:05 647470 /lib/libc-2.8.90.so
7fa69d327000-7fa69d32c000 rw-p 7fa69d327000 00:00 0
7fa69d32c000-7fa69d34b000 r-xp 00000000 08:05 647467 /lib/ld-2.8.90.so
7fa69d533000-7fa69d535000 rw-p 7fa69d533000 00:00 0
7fa69d545000-7fa69d54a000 rw-p 7fa69d545000 00:00 0
7fa69d54a000-7fa69d54b000 r--p 0001e000 08:05 647467 /lib/ld-2.8.90.so
7fa69d54b000-7fa69d54c000 rw-p 0001f000 08:05 647467 /lib/ld-2.8.90.so
7fffa5537000-7fffa554c000 rw-p 7ffffffea000 00:00 0 [stack]
7fffa55ff000-7fffa5600000 r-xp 7fffa55ff000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted
Here is my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Mult(x, y) (x)*(y)
typedef struct matrix
{
int r;
int c;
int **mtx;
}matrix;
matrix m1;
matrix m2;
void MultiplyMatrices();
int main(int argc,char * argv[])
{
FILE *input = fopen(argv[1], "r");
if (input==NULL)
{
printf("Error Opening File.\n");
return 0;
}
int rows;
int cols;
fscanf(input, "%d %d" , &rows, &cols);
//read in matrix 1
m1.r = rows;
m1.c = cols;
m1.mtx= malloc(m1.r*sizeof(int));
int i;
for(i=0; i<m1.r; i++)
{
m1.mtx[i] = malloc(m1.c*sizeof(int));
}
int j;
for(i=0; i<m1.r; i++)
{
for(j=0; j<m1.c; j++)
{
fscanf(input, "%d " , m1.mtx[i]+j);
}
}
//print matrix 1
printf("The first matrix is: \n");
for(i=0; i<m1.r; i++)
{
for(j=0; j<m1.c; j++)
{
printf(" %d ", m1.mtx[i][j]);
}
printf("\n");
}
//read in matrix 2
fscanf(input, "%d %d" , &rows, &cols);
m2.r = rows;
m2.c = cols;
m2.mtx= malloc(m2.r*sizeof(int));
for(i=0; i<m2.r; i++)
{
m2.mtx[i] = malloc(m2.c*sizeof(int));
}
for(i=0; i<m2.r; i++)
{
for(j=0; j<m2.c; j++)
{
fscanf(input, "%d " , m2.mtx[i]+j);
}
}
//print matrix 2
printf("The second matrix is: \n");
for(i=0; i<m2.r; i++)
{
for(j=0; j<m2.c; j++)
{
printf(" %d ", m2.mtx[i][j]);
}
printf("\n");
}
MultiplyMatrices();
//free memory
for(i=0;i<m1.r;i++)
{
free(m1.mtx[i]);
}
free(m1.mtx);
//I think these should be here, but it says I double deleted them.
for(i=0; i<m2.r;i++)
{
free(m2.mtx[i]);
}
free(m2.mtx);
fclose(input);
}
^^ the two bold and underlined lines are the ones that cause the error.
I took out MultiplyMatrices function as it didn't change anything. Thanks for any help,
Craig.