Freeing memory

This is a discussion on Freeing memory within the C Programming forums, part of the General Programming Boards category; Hello! I have a doubt about how the operation of freeing memory works. If i write a program like this ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    12

    Freeing memory

    Hello!
    I have a doubt about how the operation of freeing memory works.
    If i write a program like this one:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct orderlistNode {
    	int number;
    	struct orderlistNode* nextNode;
    };
    
    typedef struct orderlistNode* orderNode;
    
    int main(){
    
    	orderNode a = malloc(sizeof(struct orderlistNode));
    	orderNode b = malloc(sizeof(struct orderlistNode));
    	a->number = 47;
    	b->number = 2;
    	a->nextNode = b;
    
    	printf("a: %d\n", a->number);
    	printf("b: %d\n", b->number);
    
    	free(a);
    
    	printf("again a: %d\n", a->number);
    	printf("through a: %d\n", a->nextNode->number);
    	printf("again b: %d\n", b->number);
    
    	return 0;
    }
    even after freeing the variable 'a' I'm able to read it and even to navigate the list through its internal pointer! Is it normal? And if it is, how can I free the variable 'a' in a way that it can't be accessed anymore?
    Thanks for your help!

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Yes. Freeing memory does not automatically zero out its contents. It will be reused when the system needs it. It should NOT be depended upon to retain any data though. Things could change in there any time because it has been designated as available.

    Computers save time by not writing any superfluous data, such as zeroes, to storage that is marked as "free". Just like on hard-drives, where old data can sometimes be recovered from so called erased files. Why write stuff when there's usually no expectation that anyone would want to read it.
    Last edited by nonoob; 12-15-2010 at 07:04 AM.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    how can I free the variable 'a' in a way that it can't be accessed anymore?
    Code:
    free(a);
    a = NULL;
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    BEFORE you free that memory you should zero it out. Being careful to only assign the correct size of data. Could be a = 0 (for int), or memset(&a, 0, 1000) for 1000 bytes worth. Then you use free().

    For your example:
    Code:
    memset(a, 0, sizeof(struct orderlistNode));
    free(a);
    Last edited by nonoob; 12-15-2010 at 07:30 AM.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    Thanks for all the answers!

    Quote Originally Posted by nonoob View Post
    BEFORE you free that memory you should zero it out. Being careful to only assign the correct size of data. Could be a = 0 (for int), or memset(&a, 0, 1000) for 1000 bytes worth. Then you use free().

    For your example:
    Code:
    memset(a, 0, sizeof(struct orderlistNode));
    free(a);
    This is to avoid that, if the memory is later assigned to another variabile, the variable will be pre-allocated with the old values in that memory space?

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    This is to avoid that, if the memory is later assigned to another variabile, the variable will be pre-allocated with the old values in that memory space?
    Maybe or maybe not. Anyway you SHOULD NOT be relying on such behavior.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    That is correct. When allocating memory you should never assume there are retained "old values" there. You must simply initialize the space with your own data after a malloc.

    I thought what Soel was after was ensuring any old data was no longer accessible... possibly to do with data security. A destructive delete. Other than that there is no reason to zero out data just prior to free().
    Last edited by nonoob; 12-15-2010 at 11:29 AM.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Quote Originally Posted by nonoob View Post
    Yes. Freeing memory does not automatically zero out its contents. It will be reused when the system needs it. It should NOT be depended upon to retain any data though. Things could change in there any time because it has been designated as available.

    Computers save time by not writing any superfluous data, such as zeroes, to storage that is marked as "free". Just like on hard-drives, where old data can sometimes be recovered from so called erased files. Why write stuff when there's usually no expectation that anyone would want to read it.
    Could this come back to bite you later?

    My situation, in a string manipulation function, is that I make some copies of strings with strncpy or strncat into a string called second_str and so forth. I would then free said string of size n at the end of the function.

    I would later make repeated calls to that function and malloc space for the same string name of same size n called second_str, but instead copy n-3 chars into it. Upon printing it, i would see the original last 3 chars of the previous string when the function was used previously.

    Could this be why it does that?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you're seeing old garbage values when you print strings, it's because you don't actually have a string. Translation: You forgot your '\0'.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    Thanks again!
    One last question: freeing my variable 'a' in the example of the first post doesn't free also the 'b' variable pointed by its internal field, am I right?

  11. #11
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    One last question: freeing my variable 'a' in the example of the first post doesn't free also the 'b' variable pointed by its internal field, am I right?
    Yes,
    Edit : each malloc/calloc call need corresponding free() call.
    Last edited by Bayint Naung; 12-16-2010 at 05:39 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Freeing memory from vector of structs
    By onako in forum C++ Programming
    Replies: 15
    Last Post: 07-31-2010, 10:12 AM
  2. memory freeing function
    By johndoe in forum C Programming
    Replies: 4
    Last Post: 02-17-2006, 01:08 AM
  3. Freeing memory for non-pointer variables
    By SuperGodAntMan in forum C++ Programming
    Replies: 7
    Last Post: 02-11-2006, 12:30 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. memory allocation and freeing
    By Jase in forum Linux Programming
    Replies: 1
    Last Post: 05-25-2003, 06:26 AM

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