Fixing up memory leaks

Posted 09 July 2011 - 12:30 PM

I am trying to do copy constructors and I can get them to work if I don't try to delete anything. When I run this code it works unless I initialize an empty linked list and try to use the copy constructor. How do I get the if condition to recognize that it doesn't need to delete a Node that doesn't exist?

It keeps seg faulting at the (if node->next!=NULL) because for some reason the condition for if(first!=NULL) doesn't work for this empty list. Why? Any tips or suggestions on how to clean this up so that my conditions work would be aprreciated

Oh, PS this post was called fixing up memory leaks because if I don't call delete node; it works just fine.

Replies To: Fixing up memory leaks

Re: Fixing up memory leaks

Posted 09 July 2011 - 01:00 PM

using recursion in linked lists is actually a bad idea. works great if your compiler will optimize for tail recursion and you properly setup your recursion to use tail recursion; but otherwise it results in eating up mass amounts of stack space.

since in this case there is no logical (i.e. make the code easier to follow) reason to use recursion why not just use iteration:

Re: Fixing up memory leaks

Posted 09 July 2011 - 02:20 PM

NickDMax, on 09 July 2011 - 01:00 PM, said:

using recursion in linked lists is actually a bad idea. works great if your compiler will optimize for tail recursion and you properly setup your recursion to use tail recursion; but otherwise it results in eating up mass amounts of stack space.

since in this case there is no logical (i.e. make the code easier to follow) reason to use recursion why not just use iteration:

Re: Fixing up memory leaks

Posted 09 July 2011 - 02:55 PM

what doesn't work is when I get to this condition:

while(node!=NULL)
{
temp = node->next;

even though "node" is NULL it ignores the conditional and then segfaults when it trys to assign node->next to temp. It works fine when there is something to delete but when the list is empty it blows up in a giant fire-ball of death.

Re: Fixing up memory leaks

Posted 09 July 2011 - 04:51 PM

doothedew, on 09 July 2011 - 10:55 PM, said:

what doesn't work is when I get to this condition:

while(node!=NULL)
{
temp = node->next;

even though "node" is NULL it ignores the conditional and then segfaults when it trys to assign node->next to temp. It works fine when there is something to delete but when the list is empty it blows up in a giant fire-ball of death.

If your program reaches temp=node->next; then it means 'node' has a non-null value, and is probably a "dangling" pointer which was either never initialised, or an object it used to point to had been prematurely deleted.

as KYA suggested, you'll need to post your complete code because the problem doesn't appear to be with your cleanup - something else must have silently gone wrong some time before you reached this point.

One thing you could do is to ensure that your node::next is always initialised to something half-sensible as soon as the object is created (leaving 'next' uninitialised will certainly cause problems like this)