Thread: How to deallocate space with free.

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    How to deallocate space with free.

    Why my last printf does not appear when i run the program?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void){
    	char *buf;
    		buf=(char *)malloc(50*sizeof(char));
    		if(buf == NULL){
    			printf("Not enought memory to allocate buffer.\n");
    			exit(1);
    		}
    			printf("String was successfully allocated.\n");
    			
    		free(buf);
    		if(buf == NULL){
    			printf("Successfull free.\n");
    		}
    		
    return 0;
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    free() does not set the pointer to NULL (which it couldn't do anyway) -- you pass the pointer by value to free().

    There is not standard way to check if free "succeeded". Nor is there any reason to, if it fails... not your problem :-)

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Because free is not supposed to set the pointer to null (actually, since you're passing the value of the pointer to the function, it's actually impossible for it to set your copy to null, but that's another issue). All it does is deallocate the memory. When you allocated it, it set it aside so that only you could use it - no one else could allocate the same block of memory. When you free it, it's no longer set aside. Your pointer hasn't changed, but you no longer have any guarantee that the memory isn't already in use.

  4. #4
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Smile

    Once you free the buff, whatever it was pointing to is gone forever....

    So I think after free(buff) now it points to nothing.

    Nothing will be printed in the last statement, unless by coincidence buff is
    pointing at a NULL. Then you'll get the last printf() statement.

    I'm sure you'll get a lot more expert details soon. :-)

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    thank you all. from now on i' ll just free the space and don' t care further...about what exactly is happening

  6. #6
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Cool

    Let's just imagine, for a second, that free() did return a numeric value if pass/fail. Let's say NULL
    is a fail.

    Then I would write the code this way:

    Code:
    int test;
    
    test = free(buf);
    
    if(test != NULL) {
    
    printf("Successfull free.\n");
    
    return (whatever)
    }
    Last edited by Char*Pntr; 08-24-2010 at 10:10 PM.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    Smile

    Or this one...
    Code:
    if(free(buf) != NULL) {
    
    printf("Successfull free.\n");
    
    return (whatever)
    }
    i hope i am not wrong! thank you...

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    i hope i am not wrong!
    I don't know why Char*Pntr posted the code he did, because it's based on pretending that free() returns a value. free() does not return a value, and provides no way for you to check if the call was successful or failed. In most situations, you don't really care if free failed or succeeded. If free() is failing often enough for you to care, you should really consider getting a new operating system.

  9. #9
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Question

    Code:
    Quote: Let's just imagine, for a second, that free() did return a numeric value if pass/fail. Let's say NULL is a fail.
    I was trying to explain the fact that the last printout statement would most likely
    never be printed because of the construct of the if() statement even if free() did return a value.
    I thought I made that perfectly clear... I think most people (brack) understood my point.

    So next time if I want to describe a hypothetical argument, I will use "Let's pretend..." instead
    of "Let's imagine..." so that everyone here will understand. :-)
    Last edited by Char*Pntr; 08-25-2010 at 02:53 PM.

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    127
    Quote Originally Posted by brack View Post
    thank you all. from now on i' ll just free the space and don' t care further...about what exactly is happening
    Just set the pointer to NULL after you free to save yourself a lot of headaches.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by brack View Post
    Why my last printf does not appear when i run the program?
    Code:
    free(buf);
    if(buf == NULL){
    printf("Successfull free.\n");
    As the others have pointed out free doesn't return a value, you have to trust it.
    If you are working on complex code and want a confirmation that free was called you can do this...

    Code:
    // release memory
    int Release(void* ptr)
      { free(ptr);
         ptr = NULL;
         return 1; }
    
    // main code
    if (Release(MyMem))
      printf(....
    Last edited by CommonTater; 08-26-2010 at 10:58 AM. Reason: sloppy proof reading

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > If you are working on complex code and want a confirmation that free was called you can do this...
    Did you test it?
    All you managed was to make your local variable (ptr) NULL, not the value of MyMem in main()
    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.

  13. #13
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Thumbs up

    Brack, I think your post was excellent and I've learned a lot from the replies. You have intellectual curiosity, and like me, dare to experiment with the code we learn.

    A few weeks ago when the book that I was using covered malloc() and the free() functions. I tried to do something similar: I allocated some memory to store and print out "this is a test message." It worked fine. Then I wondered, what if I don't use the free(). That string must still exist somewhere in memory... would I be able to go back to verify this? So I created a pointer and initialized it to the string right after malloc().

    However, later in main(), I was unsuccessful printing out the original string. I did not invoke free(), so I would think that it still exists somewhere. I'm 3/4 of the way through my C book, so when I finish it, I will revisit this question.... and if it still wont work, I'll post a question.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Char*Pntr View Post
    However, later in main(), I was unsuccessful printing out the original string. I did not invoke free(), so I would think that it still exists somewhere. I'm 3/4 of the way through my C book, so when I finish it, I will revisit this question.... and if it still wont work, I'll post a question.
    Could you post what you've tried? Because of course it still exists in memory until you free it. But how did you try to find it back? The normal way is to keep a pointer to the allocated memory (in fact, that's the only way except maybe in the case of exploits that sometimes use "hacky" methods to find an address).

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The point is that the data may still exist in memory, even after you free it - but that memory is (hopefully), listed back in the pool of available memory. The operating system may use it in a micro-second, or it may not use it for years on end, if it's just idling away.
    It's entirely up to the OS.

    There is nothing preventing a smart compiler from freeing memory that you have not, if it determines that the memory in question, is never accessed again by your program.
    Last edited by Adak; 08-26-2010 at 03:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-18-2010, 07:25 AM
  2. free space on unknow device
    By hallo007 in forum C Programming
    Replies: 1
    Last Post: 12-27-2009, 07:16 PM
  3. Unreal Development Kit & Unity3d: Free (as in beer)
    By Mad_guy in forum General Discussions
    Replies: 0
    Last Post: 11-05-2009, 01:40 PM
  4. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 02:27 PM
  5. Why should I use the Free Space (heap)
    By BlackSlash12 in forum C++ Programming
    Replies: 8
    Last Post: 09-09-2007, 06:57 PM