Thread: Help with linked list destructor

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    16

    Help with linked list destructor

    Hey, I have hit a brick wall in figuring out why my list destructor doesn't work. I'm pretty sure I'm deleting the list correctly but I'm unsure on how I should also free the memory of the linked list.

    Here's my code for the destructor function

    Code:
    void releaseTB (TB tb) {
    
    	TB curr = tb;
    	TB tmp;
    
    	while (curr != NULL) {
    		tmp = curr->next;
    		free(curr->item);
    		curr = tmp;
    	}
    	
    }
    Basically the code is meant to free the memory occupied by the list and that it's an error to access the list once I've called this function. The item is a malloced string.

    Can anyone give me pointers on how I can fix the problem with my destructor function?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If the nodes themselves are malloc'ed, then you need to free(curr) as well.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    Even if I free(curr) as well inside the loop, I'm still able to access the list.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, yes, you will be able to access where the list used to be. After you call the function, you'll have to set the pointer tb to NULL yourself; that doesn't happen automatically. (Notice that the function can't do it, since any changes to the local variable do not persist outside the function.)

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    Ahh thanks. I'll give that a try. Because tb is the actual list or the head of the list and it's a global variable.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    But the thing is.. even if I did... if I did like a count function for how many links I have in the linked list.. it still works even if I did tb = NULL after the loop. And I can't reset tb to NULL after the function because it's an ADT I'm implementing.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by iAndy_ View Post
    But the thing is.. even if I did... if I did like a count function for how many links I have in the linked list.. it still works even if I did tb = NULL after the loop. And I can't reset tb to NULL after the function because it's an ADT I'm implementing.
    That suggests something amiss in your count function (unless you are storing the count somewhere in a struct variable). And of course a list is an ADT -- that has no bearing whatsoever on your ability to set a pointer value to NULL.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    This is my count function

    Code:
    int linesTB (TB tb) {
    
    	TB curr = tb;
    	int lines = 0;
    	
    	while(curr != NULL) {
    		lines++;
    		curr = curr->next;
    	}
    	
    	return lines;
    
    }

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And now I see what you said:
    it still works even if I did tb = NULL after the loop
    You cannot Cannot CANNOT try to change tb inside that function -- any changes to tb made inside the function are discarded when the function ends. You must set tb to NULL from the function where you call releaseTB, directly after the call.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    Hmmm I can't do what you told me to do.. since... The main is inside a client and I don't have access to the client.. otherwise I would of done that. Is there another approach to this problem?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Technically that means it's not your problem (unless your specs say that the list should exist after deletion), but you can have a "dummy" node at the front of the list that never gets deleted (and therefore doesn't get counted, etc) whose only job is to always be there and be the start of your list.

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    Hmmm I can't do what you told me to do.. since... The main is inside a client and I don't have access to the client.. otherwise I would of done that. Is there another approach to this problem?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by iAndy_ View Post
    Hmmm I can't do what you told me to do.. since... The main is inside a client and I don't have access to the client.. otherwise I would of done that. Is there another approach to this problem?
    You can have a "dummy" node at the front of the list that never gets deleted (and therefore doesn't get counted, etc) whose only job is to always be there and be the start of your list.

  14. #14
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    Hmmm I can't do what you told me to do.. since... The main is inside a client and I don't have access to the client.. otherwise I would of done that. Is there another approach to this problem?

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by iAndy_ View Post
    Hmmm I can't do what you told me to do.. since... The main is inside a client and I don't have access to the client.. otherwise I would of done that. Is there another approach to this problem?
    If the client was involved in any way, shape, or form, I wouldn't have suggested it. If the list must give meaningful results after being emptied, then you must create some sort of permanent structure that doesn't contain actual data, but merely exists for bookkeeping purposes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By mikeman in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 01:56 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM