Thread: double free or corruption (out)

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    double free or corruption (out)

    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.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    In this,
    Code:
    m1.mtx= malloc(m1.r*sizeof(int*));
    	int i;
    	for(i=0; i<m1.r; i++)
    	{
    		m1.mtx[i] = malloc(m1.c*sizeof(int));
    	}
    You intend to create an array of int pointers, such that mtx[i][0] is the first element of a row pointed to by mtx[i]. But you don't do that, you create an array of ints. I added the red asterisk to correct this. Since they could be the same size, it may be okay...

    But that is not your double free() error
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If I comment out the matrix multiply, I get printed matrices without any seeming fuss or muss or complaints from libc. Perhaps it is in your MultiplyMatrices?

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    Weird, It gave me that error before I implemented the multiplication. I commented it out and even cut it out and I still get the error. I'm pretty new to c, I'm not real sure what to do. I swear I've looked over this code 1000 times and m1 and m2 are identical, but only m2 gives me the error.

    Thanks for the replies.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    m1.mtx= malloc(m1.r*sizeof(int))ж

    should be sizeof (int*) or better

    sizeof *m1.mtx

    same for the second matrix
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    Thanks vart, that worked. I remember doing that in lab now that you mention it. I appreciate everyones help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Replies: 7
    Last Post: 11-26-2007, 01:11 PM
  4. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  5. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM