Thread: Question on free()

  1. #1
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283

    Question on free()

    I come from a C++ background where 'delete' was different if you're working with an array of memory. With free(), does it delete the array automatically?

    Code:
    #include <stdio.h>
    #include <stdlib.h>   
    #include <string.h>  
    #include "actor.h"
    #include "stack.h" 
    
    int main(void)
    {         
    	ASTACK stkActors;    
    	ACTOR PC, PC2, PC3;
    	ACTOR actTemp;
    
    	/* Initialize the actors */
    	Actor_Init(&PC, "Bob", 1);
    	Actor_Init(&PC2, "Bobby", 2);
    	Actor_Init(&PC3, "Other Bob", 3); 
    
    	/* Initialize the dynamic stack of 32 actors */
    	AStack_Init(&stkActors, 32);
    
    	/* Push actors into the stack. */
    	AStack_Push(&stkActors, &PC);
    	AStack_Push(&stkActors, &PC2);
    	AStack_Push(&stkActors, &PC3);
    
    	/* Pop away! */
    	AStack_Pop(&stkActors, &actTemp);
    	Actor_Show(&actTemp);
    
    	AStack_Pop(&stkActors, &actTemp);
    	Actor_Show(&actTemp);
    
    	AStack_Pop(&stkActors, &actTemp);
    	Actor_Show(&actTemp); 
    
    	/* Release the stack memory */
    	AStack_Release(&stkActors);
     
    	return 0;
    }
    (Just showing the code because it's beautiful).

    Actually, on AStack_Release(&stkActors), I call free() this way:

    Code:
    void AStack_Release(ASTACK *s)
    {
    	if (s->p_stck != NULL)
    		free(s->p_stck);
    	
    	s->p_stck = NULL;
    }
    Is that right? And I heard free() implicitly checks for null so the condition isn't necessary. Is this correct? Thanks.

    PS. Just a side note, how's this C coding style above that I developed for C coding? I think OO helped me to be clear more like myStructureType_DoSomething(&myStructure, ...).
    Last edited by dxfoo; 07-14-2010 at 10:31 PM.

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    1) I don't see an array. . .

    2) free(NULL) results in undefined behavior and is uber compiler dependent as to what happens. IIRC, gcc will allow this and then the system will seg fault on Linux.

  3. #3
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    p_stck is the dynamic array.

    If I'm understand that right, it's always good to check for null before freeing then just to be on the safe side.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    free(NULL) is benign and works on every compiler that I know of.

    On the other hand, your code may leak depending on what Actor_Init does. If it allocates memory dynamically for an ACTOR, then doing free on the stack's array isn't enough: You'll need to traverse the whole stack and deallocate the resources for each ACTOR.
    Stick close to your desks and never program a thing,
    And you all may sit in the standards commitee!

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Define p_stck -- there's not enough information.

    YES. Always check for NULL before you call free().

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Kennedy View Post
    Define p_stck -- there's not enough information.

    YES. Always check for NULL before you call free().
    No, Don't. free(NULL) is well-defined by the standard to do nothing.

    From the C89 standard draft:
    ...
    The free function causes the space pointed to by ptr to be
    deallocated, that is, made available for further allocation. If ptr
    is a null pointer, no action occurs. Otherwise, if the argument does
    not match a pointer earlier returned by the calloc , malloc , or
    realloc function, or if the space has been deallocated by a call to
    free or realloc , the behavior is undefined."
    Last edited by zacs7; 07-14-2010 at 11:10 PM.

  7. #7
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    Can't argue there. Thanks!

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    Quote Originally Posted by zacs7 View Post
    No, Don't. free(NULL) is well-defined by the standard to do nothing.
    It's also one of those areas where the implementations commonly get it wrong.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    I think checking the compiler's documentation is the best bet on a null pointer. But it wouldn't hurt adding a condition some programmers expect, just for the sake of not getting into this arguement. I'm already use to checking for null pointers elsewhere.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,172
    Quote Originally Posted by brewbuck
    It's also one of those areas where the implementations commonly get it wrong.
    I have heard of this assertion, but how true is it these days, some twenty years after the C standard was first published? After all, getting the code right in itself should not be difficult, but knowing that the behaviour is supposed to be that may be another matter, but it should now be well known.
    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

  11. #11
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    2) free(NULL) results in undefined behavior and is uber compiler dependent as to what happens. IIRC, gcc will allow this and then the system will seg fault on Linux.
    It's totally wrong.


    YES. Always check for NULL before you call free().
    Which is un-necessary.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,493
    Some ancient pre-ANSI C compilers (especially embedded ones) may have had trouble freeing NULL.
    I also recall some PoC that didn't even do short-circuit && || evaluation properly.

    You would never get anywhere if you "coded around" every real (or imagined) bug in any compiler. If a compiler fails on this basic stuff, raise bug reports or shop around for something better.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about performance and free()
    By The Physicist in forum C Programming
    Replies: 29
    Last Post: 05-26-2010, 10:44 AM
  2. Simple question about free()
    By fanoliv in forum C Programming
    Replies: 7
    Last Post: 06-16-2006, 11:14 AM
  3. question about free()
    By TalosChen in forum C Programming
    Replies: 5
    Last Post: 05-20-2006, 06:11 PM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM