free() twice

This is a discussion on free() twice within the C Programming forums, part of the General Programming Boards category; Hello. What are the implications of free() a pointer twice? Code: .... gchar *string; .... string = strcat (....) free(string); ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    38

    free() twice

    Hello.

    What are the implications of free() a pointer twice?


    Code:
    ....
    gchar *string;
    ....
    string = strcat (....)
    
    free(string);
    free(string);
    .....
    Last edited by guillermoh; 02-07-2008 at 07:26 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    That's undefined behavior.

  3. #3
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Just don't do it.

    Also, don't free() a pointer which has not been malloc()'ed
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by guillermoh View Post
    Hello.

    What are the implications of free() a pointer twice?


    Code:
    ....
    gchar *string;
    ....
    string = strcat (....)
     
    free(string);
    free(string);
    .....
    Why dont you try and see. You get a lump of memory map errors .

    ssharish

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Usually it goes boom!

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by ssharish2005 View Post
    Why dont you try and see. You get a lump of memory map errors .

    ssharish
    because it is undefined behaviour - try-and-see approach is not very good in this case. Someone could be so unlucky that in his "tests" everithing will seem OK... And only after implementing this "works for me" code in the real project the long expected "boom" will occure
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,532
    guillermoe, NULL free()'s argument so that subsequent calls don't matter.
    Code:
    type * p = foo(); // presume foo allocates types 
    // use p . . .
    free( p );
    p = NULL;
    free( p ); // harmless

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by citizen View Post
    guillermoe, NULL free()'s argument so that subsequent calls don't matter.
    As long as you do not have a copy of the malloced pointer
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,532
    Yeah. Stray pointers are a problem regardless, though: that's not unique to malloc'd stuff. A pointed-to object could just as easily fall out of scope beforehand, and something's done with it. Whether that makes the advice bad, I'm not ready to say.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As to exactly what happens when you free twice, it is very much dependent on the implementation of free(). A very likely scenario is that it will stick the same memory block twice in the list of free memory blocks - which means the first and second malloc with a matching size, it will return the same pointer.

    Another option is that free actually realizes and gives an error message.

    A further possibility is that the free causes an infinite loop in a linked list of free memory blocks, because the second free causes the block to point to itself, and next time you do a malloc of a bigger block of memory, malloc() tries to walk the list and gets stuck in an endless loop.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  2. Help needed with backtracking
    By sjalesho in forum C Programming
    Replies: 1
    Last Post: 11-09-2003, 05:28 PM
  3. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 01:10 AM
  4. SIGABRT upon free()
    By registering in forum C Programming
    Replies: 2
    Last Post: 07-19-2003, 07:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21