Thread: why glibc detected *** double free or corruption (out): 0x0000002ab292b110

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    168

    why glibc detected *** double free or corruption (out): 0x0000002ab292b110

    Code:
    struct window
    {
           int chrNum;
           int csID;
           char *indicInd;
           char *japInd;
           int startPos;
           int endPos;
    };
    
    int main(int argc,char *argv[])
    {
        struct window *w = (struct window *) malloc (30);
        struct window *tmpW;
        tmpW   = wInd; 
        
        /*
        assign values to (struct window *) w
        */
        
        free(w);
        
        return 0;
    }
    run result:
    glibc detected *** double free or corruption (out): 0x0000002ab292b110

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You can't assume anything about the size of the struct, so this is wrong on so many levels:
    Code:
    malloc(30)
    Also, don't cast the return value of malloc in C code. You should also check if the call to malloc is successful or not.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You also seem to be assuming that the code you commented out wasn't part of the problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by Memloop View Post
    You can't assume anything about the size of the struct, so this is wrong on so many levels:
    Code:
    malloc(30)
    Also, don't cast the return value of malloc in C code. You should also check if the call to malloc is successful or not.
    how to malloc memory?
    malloc(30*sizeof(struct)) ; ?

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    1
    First question:
    Why you use malloc(30). From where this 30 comes. Considering 32 bit machine you would have use 24.
    On all top of that this method is wrong...!!!
    I will suggest following way.
    struct win some_name; // Declare a struct variable
    w = malloc (sizeof (some_name);
    if ( W == NULL)
    Exit with error code
    else
    continue ;
    [/I][/I][/I]

    free (w) ;
    Please fell free to point errors

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Don't you think that if you had to calculate the size of a struct yourself (which is rather tedious and error prone), that they'd make a way of doing it automatically in the language instead?
    Guess what? They did! it's called "sizeof"
    Code:
        struct window *w = malloc(sizeof(*w));
    How cool is that!

    You know what else? That's probably not the cause of the "double free or corruption" problem you're having. You need to post code that actually compiles and produces the error. Don't make up mock examples that don't actually produce the problem at all. The bug is never in the bit you think it's in, or you'd have already solved it.
    Last edited by iMalc; 01-22-2010 at 12:53 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by iMalc View Post
    Don't you think that if you had to calculate the size of a struct yourself (which is rather tedious and error prone), that they'd make a way of doing it automatically in the language instead?
    Guess what? They did! it's called "sizeof"
    Code:
        struct window *w = malloc(sizeof(*w));
    How cool is that!

    You know what else? That's probably not the cause of the "double free or corruption" problem you're having. You need to post code that actually compiles and produces the error. Don't make up mock examples that don't actually produce the problem at all. The bug is never in the bit you think it's in, or you'd have already solved it.
    Your guess is right! Because my codes is long very much, so I make up this example。 I have thougt error was in it.

    I have solved this error. Thans everyone for helps!

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by iMalc View Post
    Code:
        struct window *w = malloc(sizeof(*w));
    Dereferencing an uninitialized pointer? Isn't that kinda unsafe?

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by _Mike View Post
    Dereferencing an uninitialized pointer? Isn't that kinda unsafe?
    If you'd do anything with it, yes. Not if you simply pass it to sizeof, which doesn't even dereferences it but rather finds out the type of the dereferenced pointer. Although I usually use sizeof(type), because I prefer that readability.

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Ah yes you are right. I did some tests and it seems sizeof() is evaluted at compile time. I had incorrectly assumed it was done at runtime.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by _Mike View Post
    Ah yes you are right. I did some tests and it seems sizeof() is evaluted at compile time. I had incorrectly assumed it was done at runtime.
    It depends on what you use it on. C99 thwarts that concept by adding variable length arrays.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Didn't know that was specific to C99. Anyway, eg:
    Code:
    #include <stdio.h>
    
    void test(int len) {
    	int ray[len];	/* runtime value */
    	printf("-> %d ints = %lu bytes",len,sizeof(ray));
    }
    
    
    int main() {
    	int n;
    	scanf("%d",&n);
    	test(n);
    	return 0;  
    }
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  3. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  4. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  5. *** glibc detected *** double free or corruption
    By 3saul in forum C Programming
    Replies: 8
    Last Post: 02-06-2006, 12:26 PM