#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2

    Need help with freeing memory


    Hi guys.

    i need help with freeing memory from heap after allocating memory for linked list.
    i wrote a function that free the (two directional) list, but i'm gettin' runtime error everytime i use it.

    here's the code:

    Code:
    void Free_List(struct words *iterator)
    {
    	while (iterator!=NULL)
    	{
    		if (iterator->next==NULL)
    			free(iterator);
    		else
    		{
    			iterator=iterator->next;
    			free(iterator->prev);
    		}
    	}
    }
    i can also post the errors log if it's necessary.
    thanks in advanced!

    *edit

    the function gets the first Node's address.
    Last edited by so.very.tired; January 16th, 2013 at 06:49 AM.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,384
    Rep Power
    1871
    > if (iterator->next==NULL)
    > free(iterator);
    Shouldn't you set iterator to NULL, so your loop exits?

    How about
    Code:
    	while (iterator!=NULL)
    	{
    		void *temp = iterator;
    		iterator=iterator->next;
    		free(temp);
    	}
    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
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    thanks salem.
    Originally Posted by salem
    > if (iterator->next==NULL)
    > free(iterator);
    Shouldn't you set iterator to NULL, so your loop exits?
    iterator will turn to NULL when it'll point to the last node. (because "iterator=iterator->next;")

    Originally Posted by salem
    How about
    Code:
    	while (iterator!=NULL)
    	{
    		void *temp = iterator;
    		iterator=iterator->next;
    		free(temp);
    	}
    shouldn't i then free temp too?
    and what does the void * means in "void *temp"?
    Last edited by so.very.tired; January 16th, 2013 at 01:37 PM.
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,384
    Rep Power
    1871
    > iterator will turn to NULL when it'll point the last node. (because "iterator=iterator->next;")
    Ah, but you don't do you - because that only happens in the else part.
    Your special case of the last node in the list does something different.

    > shouldn't i then free temp too?
    Erm, free(temp) is exactly what happens.

    > and what does the void * means in "void *temp"?
    void* is a generic pointer type.
    Since you're not actually interested in the type any more when you're about to free it, void* serves this purpose nicely.
    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
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    Perfect.
    thanks for the help and the great explanation.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    Hi salem.

    i have another question on the same subject:

    i have another (two directional) list (binary tree), and i used recursion to free it:

    Code:
    void DestroyList(struct letters *iterator)
    {
    	if (iterator!=NULL)
    	{
    		DestroyList(iterator->left);
    		DestroyList(iterator->right);
    		free(iterator);
    	}
    }
    but i'm affraid it might cause a stack overflow since there might be over 200 letters in that list.
    would you suggest to use iteration instead?
  12. #7
  13. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,384
    Rep Power
    1871
    > but i'm affraid it might cause a stack overflow since there might be over 200 letters in that list.
    Well if you also used recursion to build the tree in the first place, then there is probably nothing to worry about.

    A perfectly balanced tree with 200 elements in it is only 8 levels deep.
    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
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    Thanks.
    i'll use recursion then. :)
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Location
    Santa Clara, CA
    Posts
    10
    Rep Power
    0
    > NULL, it will tell you when the last node. (Because "Iterator iterator = -> Next;")
    Ah, but you do not - because it's just most of the time.
    A special case of the last node of the list is to do nothing.

    > I had to open the temperature too?
    Um free (temp) This is exactly what happens.

    > And in the empty void * '* temp "?
    Cancel * pointer to a generic type.
    Since you are not really interested in the sea when you are going to be released, void * used to it very well.

IMN logo majestic logo threadwatch logo seochat tools logo