By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

Cleaning up a data structure

P: 63
Expand|Select|Wrap|Line Numbers
  1. struct node
  2. {
  3.     node(char * product):next(NULL)
  4.     {
  5.         item = new char[strlen(product)+1];
  6.         strcpy(item, product);
  7.     }
  8.     ~node()
  9.     {
  10.         delete item;
  11.         delete next;
  12.     }
  14.     node * next;
  15.     char * item;
  17. };
Lets say I had a linked list with stated the node being the stated code.

When I delete a node, does that trigger the destructor? And if so, does that mean that every node behind that node in the linked list gets removed from memory since the destructor deletes the next node in the list?

Just wondering because it would be easier then going through the whole list deleting every node.
Mar 11 '07 #1
Share this Question
Share on Google+
2 Replies

Expert 2.5K+
P: 3,652
delete does call the destructor, and so deleting the next pointer would create a cascade effect, deleting the entire list.

Are you sure this is what you want to do?
Mar 11 '07 #2

P: 63
Are you sure this is what you want to do?
Well I was just thinking conceptually, but yeah. To remove just one node, I could just isolate that node, and then delete it. And in terms of a linked list, Id do that anyway.

Oh, and thanks for the response [thumbsup]
Mar 11 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.