Tring Vu
Tring Vu

Reputation: 253

Linked list destructor

I am working on a Linked List class. Whenever I compile, I get no errors or warnings. However, the executable stops working (using Windows Dev C++). I have deduced that the problem is my destructor.

My understanding is the destructor gets called with the delete keyword for a dynamically created object, or whenever the object goes out of scope for an object on the stack.

I think my problem occurs when my stack object (Node* result) calls the destructor, but not really sure.

Here's my header file:

    #ifndef Node_H
    #define Node_H

    class Node{
          int data;
          Node* next;

    public:
           Node(int data);       
           ~Node();
           void insert(int d);
           Node* remove(int d);
           void removeDups(void);
           Node* add(Node* a, Node* b);
           void printOut(void);
    };

    #endif

Revelant parts of .cpp file:

    #include <iostream>
    #include "Node.h"

    using namespace std;

    Node::Node(int d){
          data = d;
          next = NULL;
          }

    Node::~Node(){
           Node* n = this;
           Node* delptr;

           while(n->next != NULL){
                         delptr = n;
                         n = n->next;
                         delete delptr;
                         }
           delete n;            

        }

    void Node::insert(int d){

          Node* n = this;
          Node* current = new Node(d);

          while(n->next != NULL){
                  n = n->next;                   
                  }

          n->next = current;
          }

Main:

    int main (void){
        int i = 0;

        Node* root = new Node(111);
        Node* result;

        root->printOut();

        for (i = 0; i < 11; i++){
            root->insert(i);
            } 

        root->printOut();

        delete root; 
        getchar();
        return 0;   
    }

Also, I am running Dev C++ on a USB. My hope with that was to prevent memory leaks on my OS. Is that correct?

Thanks

-- Tring Vu

Upvotes: 1

Views: 1927

Answers (2)

Captain Giraffe
Captain Giraffe

Reputation: 14705

The key idea when writing resource management is to keep concepts and responsibilities as simple as possible.

What you have is a root node responsible for deleting all its children. You would very much prefer a structure that is responsible for the delete, than the individual node.

The common thing to do is to create a LinkedList type/class along with a Node class. This will simplify the responsibilities. The LinkedList type will own the Nodes, and as an owner be responsible for the deleting in its destructor.

inserts and remove / removeDups would then go into the LinkedList class. The node can help with functions like

insert(Node* afterThisNode){  
    this->next = afterThisNode->next;
    afterThisNode->next= this;
}

Upvotes: 1

CapelliC
CapelliC

Reputation: 60014

delete delptr;

it's surely wrong, because initially delptr == this, and you're in a destructor (i.e. code already called on deallocation request)

Upvotes: 5

Related Questions