Thread: Realloc Double Free

  1. #16
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Quote Originally Posted by mingerso View Post
    This was mentioned earlier but heres an example of realloc and not destroying your previous pointer ( so you can avoid bad free()'s and memory leaks ):

    Code:
    char * add_space(char ** str)
    {
    	int len = strlen(*str);
    	char * tmp = (char *)realloc(*str, sizeof(char)*len+2);
    	if(tmp)
    	{
                    /* allocation was ok so we have our new address to point to */
    		*str = tmp;
    	} else {
    		/* out of memory, so do something... or not */
    		free(*str);
    	}
    
    	/* now you can error check the function on NULL or not */
    	return tmp;	
    }
    Hope that helps.
    There's "*str=NULL;" missing after "free(*str);" - in case free() will be used, and return value of function ignored...

  2. #17
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    That is the typical method but depends on what you're doing. It is not required and I see your point, but if you're going to be using pointer allocations this way you might want to make a wrapper for free() that sets it for you:

    Code:
    void * safe_free(void * ptr)
    {
    	if(ptr)
    		free(ptr);
    	return NULL;
    }
    
    var = safe_free(var);

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is safe to free a null pointer, so one could write:
    Code:
    void * safe_free(void * ptr)
    {
        free(ptr);
        return NULL;
    }
    But then since this requires one to remember to assign the return value of safe_free() back to the pointer, I see no benefit over just writing:
    Code:
    free(var);
    var = NULL;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Quote Originally Posted by mingerso View Post
    That is the typical method but depends on what you're doing. It is not required and I see your point, but if you're going to be using pointer allocations this way you might want to make a wrapper for free() that sets it for you:

    Code:
    void * safe_free(void * ptr)
    {
    	if(ptr)
    		free(ptr);
    	return NULL;
    }
    
    var = safe_free(var);
    Yes, but for me that's a 2-step operation (function code, and checking return value).
    I just call a function and it sets pointer to NULL if something goes wrong, so checking return value is optional (and pointer even if free() got called, remains valid).
    Last edited by rasta_freak; 08-04-2008 at 10:09 AM.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I would prefer something like this:
    Code:
    void safe_free(void **ptr)
    { 
       if (ptr)   // Check for stupid programmer errors
       {   
          free(*ptr);
          *ptr = NULL;
       }
    }
    
    // And to complete it... 
    #define free(x) safe_free(&x)
    --
    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.

  6. #21
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Quote Originally Posted by matsp View Post
    I would prefer something like this:
    Code:
    void safe_free(void **ptr)
    { 
       if (ptr)   // Check for stupid programmer errors
       {   
          free(*ptr);
          *ptr = NULL;
       }
    }
    
    // And to complete it... 
    #define free(x) safe_free(&x)
    --
    Mats
    My style (preference?) exactly

  7. #22
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    Yes all very good points of preference... and back to what was being said:

    "... so do something... or not"

  8. #23
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    or better yet, you could just:
    Code:
    assert(ptr);
    Rather than having an IF, in that case.
    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"

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by iMalc View Post
    or better yet, you could just:
    Code:
    assert(ptr);
    Rather than having an IF, in that case.
    Yes, I agree.

    --
    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. double free or corruption (fasttop)
    By yougene in forum C Programming
    Replies: 6
    Last Post: 01-17-2009, 06:44 PM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. 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
  4. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  5. Please HELP!!
    By traz in forum C++ Programming
    Replies: 4
    Last Post: 04-14-2003, 09:20 PM