![]() |
|
#1
|
||||
|
||||
Linked list memory questionI've written a linked list class to use with another program I'm making. I wanted to make sure that I clean up all the used memory. I added a destructor to my class. Will that be enough to clear the memory up? I've tried to add delete statements at the end of each function but most of them caused the function to no longer work correctly. The following is the class definitions (The destructor is at the bottom)
CPP / C++ / C Code:
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead." -Thomas Paine www.sullivan-county.com/deism.htm |
|
#2
|
|||
|
|||
Re: Linked list memory questionQuote:
Two quickies, off the top of my head: With de-allocating memory, there are two ways to go wrong. 1. Don't delete enough (that is leave something orphaned with nothing pointing to it so that it can't be deleted when the object goes out of scope). 2. Delete too much (that is, delete something that wasn't obtained from new). For example, here's the first one: 1. In function deleteNode, as you unlink a node from the list, you must delete the memory then and there, since the destructor will not be able to find it later. And here's the second one 2. In your destructor, you have the following CPP / C++ / C Code:
You musn't delete temp1 after the loop, since, if the list is empty, you don't go through the loop at all, and you will be deleting something whose address you didn't get from new. That's a no-no. On the other hand, if the list isn't empty, the loop takes care of deleting all of the nodes in the list, so the extra delete after the loop is unnecessary. (In other words, it works OK without that last delete.) Regards, Dave |
|
#3
|
||||
|
||||
Re: Linked list memory questionThanks for the tips dave. I like pointers, hate memory management.
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead." -Thomas Paine www.sullivan-county.com/deism.htm |
|
#4
|
|||
|
|||
Re: Linked list memory questionUsually before deleteing, make habit to check it first and then use delete
|
Recent GIDBlog
Developing GUIs with wxPython (Part 4) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 07:44 |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 13:12 |
| linked list error message | Krandygrl00 | C++ Forum | 4 | 22-Jun-2005 14:13 |
| search linked list | itsmecathys | C++ Forum | 20 | 18-Apr-2005 01:34 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The