Thread: free memory from malloc

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    94

    Unhappy free memory from malloc

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
    	char *name;
    	name = (char *) malloc(80*sizeof(char));
    	if ( name != NULL ) {
    		printf("\nEnter your name: ");
    		gets(name);
    		printf("\nHi %s\n", name);
    		free(name);
    		if ( name == NULL ) {
    			printf("\nGood!");
    		}
    		else 
    			printf("\nNOT Good!");
    		        printf("\nHi %s\n", name);
    	}
    	return 0;
    }
    My output:
    Code:
    Enter your name: c_lady
    
    Hi c_lady
    
    NOT Good!
    Hi c_lady
    How come after calling the free function I am still able to print the name???

    I really don't understand this... isn't this strange?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because no other process managed, in the 0.0000006 seconds between you freeing the memory and accessing it, to reclaim it and use it for their own. The computer doesn't go around zeroing out memory all the time, it just lets it be. You are still holding a pointer to that chunk of memory, even though you don't own it, which is your real error.

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    I believe that free just makes it so that the space your string was taking is now available for use by another part of your program or another program. It doesn't change where your pointer points to. Whether or not your or another program has changed what is stored at that location is by chance. Technically, your printf after the if statement will return an undefined value.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You might want to read the FAQ's which deal with casting malloc and abuse of gets().
    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.

  5. #5
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39
    That's why some think it is good practice to set variables to NULL after freeing the pointer. I once wrote wrapper functions that keep track of malloc and free and take care of stuff like this, but it sometimes makes me lazy with malloc when I write code outside of my library.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    Quote Originally Posted by hellork View Post
    That's why some think it is good practice to set variables to NULL after freeing the pointer. I once wrote wrapper functions that keep track of malloc and free and take care of stuff like this, but it sometimes makes me lazy with malloc when I write code outside of my library.
    Do you mean something like this?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
    	char *name;
    	name = (char *) malloc(80*sizeof(char));
    	if ( name != NULL ) {
    		printf("\nEnter your name: ");
    		gets(name);
    		printf("\nHi %s\n", name);
    		free(name);
    		name==NULL;
    		if ( name == NULL ) {
    			printf("\nGood!");
    		}
    		else 
    			printf("\nNOT Good!");
    		printf("\nHi %s\n", name);
    	}
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    There're already existing discussion about setting NULL after free(). c - Setting variable to NULL after free ... - Stack Overflow
    If you read the free() doc, you won't even need to ask here why it's not set to NULL.
    free() is just a function, how could it set a pointer to NULL!
    eg.
    Code:
      
     void wrong_set_function(void *p) {
              p = NULL;    // WRONG not changed in caller
       }
    Question 4.8

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Free not freeing memory.
    By tpill90 in forum C Programming
    Replies: 14
    Last Post: 10-08-2010, 01:12 PM
  2. 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
  3. Program that displays amount of free memory
    By trancedeejay in forum Linux Programming
    Replies: 3
    Last Post: 01-13-2006, 01:27 PM
  4. When to free up memory?
    By thetinman in forum C Programming
    Replies: 7
    Last Post: 09-27-2005, 03:22 PM
  5. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM