Thread: Free()

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    7

    Free()

    When I compile this code on gcc using linux ubuntu 11.04

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    float *mult_matrix(int n, float *p, float *q);
    float *get_matrix(int m, int n);
    void show_matrix(int m, int n, float *p);
    
    int main(void)
    {
        int n;
        float *a, *b, *x;
    
        printf("\nEnter the size of the linear system: \n");
        scanf("%d", &n);
    
        a=get_matrix(n, n);
        show_matrix(n, n, a);
        b=get_matrix(n, 1);
        show_matrix(n, 1, b);
        x=mult_matrix(n, a, b);
        show_matrix(n, 1, x);
    
        free(a);
        free(b);
        free(x);
    
        return 0;
    }
    
    float *mult_matrix(int n, float *p, float *q)
    {
        int i, j;
        float *x;
    
        x=(float *)malloc((n)*sizeof(float));    
    
        if (!x) {
            printf("NULL pointer detected. Exiting ...");
            exit(1);
        }
    
        for(i=0; i<n; i++) {
            *x=0;
            x++;
        }
        x=x-n;
    
        for (i=0; i<n; i++) {
            for(j=0; j<n; j++) {
                *x=*x+(*p)*(*q);
                p++;
                q++;
                x++;
            }    
            q=q-n;
        }
        x=x-n;
    
        return x;
    }
    
    float *get_matrix(int m, int n)
    {
        int i, j;
        float *a;
        
        a=(float *)malloc((m*n)*sizeof(float));
    
        if (!a) {
            printf("NULL pointer detected. Exiting ...");
            exit(1);
        }
    
        for (i=0; i<m; i++) {
            for (j=0; j<n; j++) {
                printf("\nEnter with the element a%d%d of the matrix:", i+1, j+1);
                scanf("%f", a);
                a++;
            }    
        }
    
        a=a-m*n;
    
        return a;
    }
    
    void show_matrix(int m, int n, float *p)
    {
        int i, j;
        
        printf("\n");
        for (i=0; i<m; i++) {
        printf("\n");
            for (j=0; j<n; j++) {
                printf(" %f ", *p);
                p++;
            }
        }
        printf("\n");
        p=p-m*n;
    
    }
    I got this message on the end of the program after running it normally on the terminal:
    Code:
    *** glibc detected *** ./a.out: double free or corruption (out): 0x097d5058 ***
    It means that the program or the compiler frees the pointers automatically so I dont have to use free()? Or this dont make any sense at all? Please, I need some help. Cheers.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    No, the compiler never "frees" anything for you. You must always do that yourself.

    What that error usually means is
    1. You are trying to free something that has already been freed.
    2. You are trying to free something that has never been allocated and the pointer contains a garbage address

    You probably have an instance of case 2, since you only free in one place, and it's not in a loop, it's hard to free something twice. You most likely overwrite the value in a, b or x at some point so you no longer have the right address to pass to free.

    What input are you giving it? What output do you expect and what output do you actually get? I get a seg fault, not a double free/corruption error. It's probably related to the same bug, but I can't be sure without your input so I can try to reproduce exactly what you're doing.

    Also, have you run this through a debugger? Do you know how? Try compiling like so:
    Code:
    $ gcc -Wall -g matrix.c
    Then, load up your program in gdb
    Code:
    $ gdb ./a.out
    Run it, and keep entering input until it crashes. Then, get a backtrace to show where the crash happened
    Code:
    (gdb) run
    ...
    Program received signal SIGSEGV, Segmentation fault.
    0x080487d6 in mult_matrix (n=3, p=0xb7eedfe8, q=0xb7eefff4) at matrix.c:50
    50                  *x=*x+(*p)*(*q);
    (gdb) backtrace
    #0  0x080487d6 in mult_matrix (n=3, p=0xb7eedfe8, q=0xb7eefff4) at matrix.c:50
    #1  0x08048712 in main () at matrix.c:20
    Use up, down and print commands to move around the stack frame and examine variables to find out what went wrong.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by guitarman View Post
    I got this message on the end of the program after running it normally on the terminal:
    Code:
    *** glibc detected *** ./a.out: double free or corruption (out): 0x097d5058 ***
    It means that the program or the compiler frees the pointers automatically so I dont have to use free()? Or this dont make any sense at all? Please, I need some help. Cheers.
    It means that you are freeing something that was never allocated, presumably because you perform pointer arithmetic directly on the pointer you allocate and get it wrong.

    You could try this for example.

    Code:
    float *get_matrix(int m, int n)
    {
        int i, j;
        float *a;
         
        a=(float *)malloc((m*n)*sizeof(float));
    
    printf("%p\n", a); // <--
    
        if (!a) {
            printf("NULL pointer detected. Exiting ...");
            exit(1);
        }
     
        for (i=0; i<m; i++) {
            for (j=0; j<n; j++) {
                printf("\nEnter with the element a%d%d of the matrix:", i+1, j+1);
                scanf("%f", a);
                a++;
            }    
        }
     
        a=a-m*n;
    
    printf("%p\n", a); // <--
    
        return a;
    }

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Well, one problem I see:
    How many floats do you allocate for x? Yep, n. Now, you have nested for loops on lines 48-56. The body of the inner loop is executed n * n times, and each time you do x++ without resetting x. Thus, you start accessing memory you don't own. That's undefined behavior and a big problem.

    You do this in a strange manner. I would recommend allocating 2-d arrays so you can just use array indexing instead of your wonky (and error-prone) pointer arithmetic ju-ju. Read this: Question 6.16

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    7
    Quote Originally Posted by anduril462 View Post
    Well, one problem I see:
    How many floats do you allocate for x? Yep, n. Now, you have nested for loops on lines 48-56. The body of the inner loop is executed n * n times, and each time you do x++ without resetting x. Thus, you start accessing memory you don't own. That's undefined behavior and a big problem.

    You do this in a strange manner. I would recommend allocating 2-d arrays so you can just use array indexing instead of your wonky (and error-prone) pointer arithmetic ju-ju. Read this: Question 6.16
    Thanks for your help. I made some modifications on function mult_matrix() and the problem seems to be solved

    Code:
    float *mult_matrix(int n, float *p, float *q)
    {
        int i, j;
        float *x;
    
        x=(float *)malloc((n)*sizeof(float));    
    
        if (!x) {
            printf("NULL pointer detected. Exiting ...");
            exit(1);
        }
    
        for(i=0; i<n; i++) {
            *x=0;
            x++;
        }
        x=x-n;
    
        for (i=0; i<n; i++) {
            for(j=0; j<n; j++) {
                *x=*x+(*p)*(*q);
                p++;
                q++;
                x++;
            }    
            q=q-n;
            x=x-n; // new code
        }
    
        return x;
    }
    I'll take a look anyway on the link you posted. Cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new license-free lock-free data structure library published
    By Toby Douglass in forum Projects and Job Recruitment
    Replies: 19
    Last Post: 12-22-2009, 02:33 AM
  2. 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
  3. What's cooler than free boobs? 5 free assembly books from AMD.
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 02-13-2003, 08:22 PM
  4. the free vs.net beta aint free
    By iain in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-17-2001, 06:13 PM

Tags for this Thread