tags:

views:

201

answers:

3

I have created a class which includes a linked list and this class needs to have a destructor for deleting the linked list, which I have included however because the delete method itself calls the destructor I end up in an infinite recursive call situaction.

Here is the code:-

PolynomialNode::~PolynomialNode()
{
    /*PolynomialNode* current_node_ptr = link;
    PolynomialNode* header_ptr = link;
    int index = 0;
    if( link != NULL)
    {
        while( current_node_ptr != NULL)
        {
            index++;
            current_node_ptr = current_node_ptr->get_link();

        }

        delete_nodes( &header_ptr, 0, index);
    } */
    PolynomialNode* current_node_ptr = link;
    PolynomialNode* copy_ptr;
    while( current_node_ptr != NULL)

    {
        copy_ptr = current_node_ptr->get_link();
        current_node_ptr->set_link(NULL);
        delete current_node_ptr;
        current_node_ptr = copy_ptr;
    }
}

Note I tried this with a recursive call - intentional for deleting the linked list and I still have the same problem.

Any help would be much appreciated.

NB: I know it's a recursive call because when I step through the debugger I can see it happening.

+2  A: 

Add this line in the while loop, before delete:

if ( current_node_ptr == this ) continue;

This will allow you to avoid recursion, by skipping destruction of the node that is calling. However, you should ask yourself why the heck did this happen in the first place. I cannot answer that question, because I don't see the rest of the code.

Note: this is wrong unless you have a circular link, in case of which this would be a valid design, as long as you skip deleting this.

Kornel Kisielewicz
`==` surely? :)
Skurmedel
== looks more pretty
Dewfy
@Skurmedel, @Dewfy : Lol, this comes from answering C++ questions while working on Pascal code xD, thanks!
Kornel Kisielewicz
+10  A: 

assuming link is the pointer to the next PolynomialNode, simply

PolynomialNode::~PolynomialNode()
{
    delete link;
}

will do the right thing recursively.

However, a better solution might be to retain your while() loop but move it out of the node to the destructor of a separate class representing the linked list as a whole.

moonshadow
Or even use std::list (or std::vector, since std::list is rarely a better option)...
Adam Bowen
If it's a cyclic list, you'll make a spectacular blowup. Also, if it's more nodes than the compilers max recursive depth you'll have another spectacular blowup :P
Kornel Kisielewicz
I tried this and it worked so BIG thanks.
hairyyak
No it's not a cyclic list and I think it should be ok recursive wise because it's only 3/4 nodes at max.
hairyyak
But point taken.
hairyyak
The only problem with using std::list or std::vector is that I am attempting to do some exercises on C++ from a book - and the section is on linked lists - plus the book specifies I should use linked lists i.e. ones I've created myself.
hairyyak
Plus it's more fun this way.
hairyyak
That's is definatley going to fail if the loop is cyclic. Which the original question seems to suggest with his code.
Martin York
+1  A: 

Assuming link is the pointer to the next PolynomialNode, simply

if(this->link != NULL) delete this->link;
erick2red
Yea this is basically the same approach as Moonshadow so thanks.
hairyyak