Destructor Function for List structure

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 4
Reputation: icewizard is an unknown quantity at this point 
Solved Threads: 0
icewizard icewizard is offline Offline
Newbie Poster

Destructor Function for List structure

 
0
  #1
Jan 14th, 2008
For a given list, I would like my output to have the line "Deleting node with value ..." for each node.

My destructor function works for a 2-element list, but for a 3-element list it deletes a certain node more than once, and for a list of any size greater than 3, I get an infinite loop. I try tracing through the code, but I am not sure what is going on. Any suggestions? Thanks.
  1. #include <iostream>
  2. #include <cassert>
  3. #include "lists.h"
  4.  
  5. using namespace std;
  6.  
  7. ListNode::ListNode (int k) {
  8. myValue = k;
  9. myNext = 0;
  10. }
  11.  
  12. ListNode::ListNode (int k, ListNode* ptr) {
  13. myValue = k;
  14. myNext = ptr;
  15. }
  16.  
  17. ListNode::~ListNode () {
  18. cout << "Deleting node with value " << myValue << endl;
  19.  
  20. for (ListNode* p=this; p!=0; ){
  21. p=p->myNext;
  22. delete p;
  23. }
  24. }
Last edited by Narue; Jan 14th, 2008 at 10:25 am. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 723
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Destructor Function for List structure

 
0
  #2
Jan 14th, 2008
  1. for (ListNode* p=this; p!=0; ){
  2. p=p->myNext;
  3. delete p;
  4. }
Yea, that's kind of scary. First, when you delete p, you're calling the destructor... Second, why in the world is your node class performing a list operation? Your node shouldn't give a hoot about other nodes, that's something your list class is in a better position to deal with.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 4
Reputation: icewizard is an unknown quantity at this point 
Solved Threads: 0
icewizard icewizard is offline Offline
Newbie Poster

Re: Destructor Function for List structure

 
0
  #3
Jan 14th, 2008
I'm actually working with just one class called ListNode. I changed my destructor function to look like this.

ListNode::~ListNode () {
cout << "Deleting node with value " << myValue << endl;

//this automatically stops when p points to 0, seems to be just stepping through the list?
ListNode* p = this;
p=p->myNext;
delete p;
}

It works now, but I'm not completely sure why. It seems like the function is simply stepping through the list, but when I view the contents of the list after the deletion, there are now garbage values in all the nodes. Given that I am only working with one class, is this the right way to do it? Thanks.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC