LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-10-2006, 10:45 AM   #1
cdog
Member
 
Registered: Dec 2005
Posts: 65

Rep: Reputation: 15
Linked list free memory space at the end


I have a linked list and from it I'm taking one element at a time (list=list->next). When I reach the end I want to free the memory allocated for all the list.
 
Old 04-10-2006, 11:03 AM   #2
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
Well, you should really be deallocating memory at the time you remove an element (once you remove it, if you're not keeping a pointer to the element around, you've lost any reference you had to the memory - that's how memory leaks are born).

temp = list;
list = list->next;
delete (temp);

Your destructor should still have a routine to delete a whole list, though - google for linked list destructors, or try a website like this.

HTH
 
Old 04-10-2006, 11:19 AM   #3
cdog
Member
 
Registered: Dec 2005
Posts: 65

Original Poster
Rep: Reputation: 15
Thanks. I've already wrote a recursive destructor - I just love recursion - (didn't have time to post updates). I did all the extracting operations using a pointer and then delete all the list. Do you think is a good practice to free every node at a time or at the end? I'm working with stacks and this is something like homework. Your input is very appreciated.

Last edited by cdog; 04-10-2006 at 11:21 AM.
 
Old 04-10-2006, 12:06 PM   #4
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
Careful about asking homework questions - we're not supposed to answer

Where you delete depends a lot on what each node is designed to represent, and what you want to do with the list. Remember that after you delete one, it's gone forever, so if there's a chance you'll need whatever data it held again you might want to wait.
On principle, it's definitely good practice to have a deleteNode() function or something like that...whether you use it now, or at the end when you've traversed the whole list, depends completely on your needs. Personally, I tend to wait and let the destructor or deleteList() take care of the whole thing...I think it looks tidier if you don't have delete() calls scattered all through a program.
 
Old 04-10-2006, 12:34 PM   #5
cdog
Member
 
Registered: Dec 2005
Posts: 65

Original Poster
Rep: Reputation: 15
Excellent. My thoughts exactly. I only asked you because you said that I should delete the node the moment I got the data from it. Any other idea, without breaking the "rules of homework" ?
 
Old 04-10-2006, 12:45 PM   #6
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
Ideas? Hmmm...doubly-linked lists are fun

It makes things happier if you have one class to represent the list itself (i.e. the head node) and all assorted functions, and another class just to represent a node (this may be how you're doing it already, in which case you've a much more organized professor than I did.. >_>). Doing it that way definitely helps keep things nice and tidy...not sure what other ideas you'd need
 
Old 04-10-2006, 06:42 PM   #7
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
> Careful about asking homework questions - we're not supposed to answer
its not so that we cant answer, as it is how we answer. if you make an attempt to do the homework and have a legitimate question (like this question is) then i see no problem lending a hand.

anyhow..
if your linked list is acting as a stack you will only be adding and removing items at the end. so if that is the case then there should be no problem with deleting as you go. however if you are wanting to remove items from the middle, or remove an item from the stack but not clean it up until you are ready to delete the entire stack, you could add an active flag to the items.. when you pop the stack, if the first item is not active, then more on to the next one and so on.. then when you are done just delete all items. this further hurts the effiency of the linked list, but could be another alternative..

and i second the motion to make seperate classes for the nodes and the list. have your functions that manipulate the list in a class then have a node class that holds each items info..

good luck.
 
Old 04-10-2006, 06:45 PM   #8
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
Quote:
Originally Posted by xhi
> Careful about asking homework questions - we're not supposed to answer
its not so that we cant answer, as it is how we answer. if you make an attempt to do the homework and have a legitimate question (like this question is) then i see no problem lending a hand.
I completely agree
 
Old 04-11-2006, 08:42 AM   #9
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
It also depends on how you create the list. Sometimes I'll create "chunks" of elements for lists I know are going to be large. I'll allocate enough space for, say, 100 nodes. Then each time a new node needs to be added, I'll just give it the next available hunk of space I have (instead of calling malloc 100 times), or if none is available, create some more space. However, this also means you can't free the malloc'ed space until all the nodes occupying it have left. This isn't a big problem if your using queues or stacks, since they're taken off in a nice order. However, here you have to write some methods to handle the space allocated, which can be more hassle than it's worth. I guess it really depends what the list is used for. If it's going to go "up and down" a lot, like be full, then empty, then full, then empty, it's probably best not allocating space and freeing it, if you know it's just gonna get used right back up again. This is if you plan on using it again though, if your just going to let it sit there and get stale, I'd clean it up sometime before the object dies, especially if that object is alive for most of the program's execution.
 
Old 04-14-2006, 03:53 AM   #10
cdog
Member
 
Registered: Dec 2005
Posts: 65

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by rose_bud4201
Ideas? Hmmm...doubly-linked lists are fun

It makes things happier if you have one class to represent the list itself (i.e. the head node) and all assorted functions, and another class just to represent a node (this may be how you're doing it already, in which case you've a much more organized professor than I did.. >_>). Doing it that way definitely helps keep things nice and tidy...not sure what other ideas you'd need
That's cool, but I have another question. Is there a better way to add/remove nodes besides using different pointers for every operation (not using double linked)?

xhi, I'm only popping the nodes from the start, it seems the best way to go if I don't use double-linked.

95se, allocating from the beginning is a very good idea too, but I don't think calling malloc every time I create a new node will make my program slower, but I may be wrong.
Thank you all.
 
Old 04-14-2006, 09:35 AM   #11
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
Not really - the point of a linked list is that you can dynamically allocate just the memory you need, when you need it - which means that you'll have little chunks of data scattered all through your ram & swap space. They're only linked to each other by those pointers. Typically linked lists have sort of a railroad train feel to them (at least to me) - chunks of data directly linked to the next one, where the only way to get any specific chunk is to travel through the whole darned list.

You don't necessarily have to keep them in a series of directly linked "*next" pointers, though - you could do all sorts of fun things, like an array holding the addresses of the chunks of data, sorted binary trees, or even more entertaining - a hashtable. As long as you don't lose track of an address before you formally decallocate the memory at it (with 'delete') how you keep track of the data is only algorithmic.
 
Old 04-16-2006, 06:17 AM   #12
cdog
Member
 
Registered: Dec 2005
Posts: 65

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by rose_bud4201
Not really - the point of a linked list is that you can dynamically allocate just the memory you need, when you need it - which means that you'll have little chunks of data scattered all through your ram & swap space. They're only linked to each other by those pointers. Typically linked lists have sort of a railroad train feel to them (at least to me) - chunks of data directly linked to the next one, where the only way to get any specific chunk is to travel through the whole darned list.

You don't necessarily have to keep them in a series of directly linked "*next" pointers, though - you could do all sorts of fun things, like an array holding the addresses of the chunks of data, sorted binary trees, or even more entertaining - a hashtable. As long as you don't lose track of an address before you formally decallocate the memory at it (with 'delete') how you keep track of the data is only algorithmic.
rose_bud4201, I don't want to allocate more mem space for the array of addresses and of course you're right about allocating more than one node at a time.
My concern is that for every operation I want to do with the linked list I need a pointer because I don't want to lose the "head" and I want to free the memory at the end.
 
Old 04-17-2006, 08:25 AM   #13
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
Quote:
My concern is that for every operation I want to do with the linked list I need a pointer because I don't want to lose the "head" and I want to free the memory at the end.
Yep, pretty much. You either end up passing around &head all the time, or if your class which contains all of the functions you'll be using on the list also contains a *head pointer, then it's a local member variable and all's made easier
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
free() does not clear memory space in LINUX xinminhua Programming 32 08-18-2006 03:43 PM
Linked List - Free Nodes? Mistro116@yahoo.com Programming 3 12-04-2005 05:09 PM
Not enough free space on hard drive with 50g of free space??? auoq SUSE / openSUSE 5 10-13-2004 08:21 PM
how to free linked list containers? rgiggs Programming 3 07-30-2004 01:24 PM
Formating free space: WinXP pro and RH9 dualboot with free space on 3rd drive Vermicious Linux - General 2 03-22-2004 05:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration