vendredi 18 mai 2018

C++ - linked list - copy constructor

I am trying to implement copy constructor and I have strange problem. Could you point what I am doing wrong?

I get: double free or corruption (out): 0x08936a48

Wrong output:

Constructor called...
MyList:
10 --> 20 --> 30 --> 40 --> 
MyList2:
10 --> 20 --> 30 --> 40 --> 
Destructor called...

Code:

#include <iostream>

template <typename T>
class SingleLinkedList{
protected:
    struct Node{
        T data;
        Node * next;
        Node() : next(nullptr) {}
        Node(const T num) : data(num), next(nullptr) {}
    };

private:
    Node * head;
    Node * tail;

public:
    SingleLinkedList();                                             // constructor
    ~SingleLinkedList();                                            // destructor
    SingleLinkedList(const SingleLinkedList &object);               // copy constructor
    SingleLinkedList& operator=(const SingleLinkedList &object);    // copy assignment

    void insert(T const& value);
    void displayList(std::ostream& stream = std::cout) const;

template <typename T>
SingleLinkedList<T>::SingleLinkedList() : head(nullptr), tail(nullptr){
    std::cout << "Constructor called..." << std::endl;
}

template <typename T>
SingleLinkedList<T>::~SingleLinkedList(){
    std::cout << "Destructor called..." << std::endl;
    int index = 1;
    Node * temp = nullptr;
    while(head!=nullptr){
        temp = head;
        head = head->next;
        delete temp;
        //std::cout << "Node number: " << index << " destroyed" << std::endl;
        index++;
    }
}

template <typename T>
SingleLinkedList<T>::SingleLinkedList(const SingleLinkedList<T> &oldList){
    SingleLinkedList<T> * newList = new SingleLinkedList<T>();

    // is it necessary? my constructor by default initializes head and tail with nulls
    head = nullptr;
    tail = nullptr;

    Node * temp = nullptr;
    temp = oldList.head;

    while(temp!=nullptr){
        newList->insert(temp->data);
        temp = temp->next;
    }
}


template <typename T>
void SingleLinkedList<T>::insert(T const& value){
    Node * temp = new Node(value);
    //temp->data = value;
    //temp->next = nullptr;


    if(head==nullptr){
        head = temp;
        tail = temp;
    }
    else{
        tail->next = temp;
        tail = temp;
    }
}

template <typename T>
void SingleLinkedList<T>::displayList(std::ostream& stream) const{
    Node * temp = nullptr;
    temp = head;

    while(temp!=nullptr){
        stream << temp->data << " --> ";
        temp = temp->next;
    }
    stream << std::endl;
}

int main(){

    SingleLinkedList<int> * myList = new SingleLinkedList<int>();
    SingleLinkedList<int> * myList2 = myList;

    myList->insert(10);
    myList->insert(20);
    myList->insert(30);

    myList2->insert(40);

    std::cout << "MyList:" << std::endl;
    myList->displayList();

    std::cout << "MyList2:" << std::endl;
    myList2->displayList();

    delete myList;
    delete myList2;

    return 0;
}

My "algorithm": 1. Create new list. 2. For each node of old list get data and insert it to new list.

I don't understand how using two different list I am deallocating the same part of memory. I am a student, not pro. I am asking for your understanding.

Aucun commentaire:

Enregistrer un commentaire