alekscooper
alekscooper

Reputation: 831

Copy constructor for a linked list leads to a memory error

I'm writing my own linked list class (for educational purposes) and here it is:

My code

#include <iostream>

using namespace std;

#define PRINT(x) #x << " = " << x << " "

struct ListNode {
  int val;
  ListNode* next = nullptr;
  ListNode(int x) : val(x), next(nullptr) {}
};

class LinkedList {
private:
  ListNode* _head;
  unsigned long long int _size;
public:

  LinkedList() :_head(nullptr), _size(0) {}

  LinkedList(ListNode* _h) :_head(_h), _size(0) {
    ListNode* node = _head;
    while (node != nullptr) {
      _size++;
      node = node->next;
    }
  }

  // Copy constructor
  LinkedList(const LinkedList& obj) {
    ListNode* node = obj._head;
    while (node != nullptr) {
      this->add(node->val);
      node = node->next;
    }
  }

  ~LinkedList() {
    while (_head != nullptr) {
      remove();
    }
  }

  void add(const int& value) {
    ListNode* node = new ListNode(value);
    node->next = _head;
    _head = node;
    _size++;
  }

  int remove() {
    int v = _head->val;
    ListNode* node = _head;
    _head = _head->next;
    delete node;
    _size--;
    return v;
  }

  void print() {
    if (size() == 0) {
      cout << "List is empty" << endl;
      return;
    }
    ListNode* node = _head;
    while (node->next != nullptr) {
      cout << node->val << " -> ";
      node = node->next;
    }
    cout << node->val << endl;
  }

  unsigned long long int size() { return _size; }
  ListNode* head() { return _head; }
};

int main() {

  LinkedList L;
  L.add(4);
  L.add(3);
  L.add(2);
  L.add(1);
  L.print();

  LinkedList L2(L);

  return 0;
}

The problem is that when I run this code, I get this error: error for object 0x7fff5b8beb80: pointer being freed was not allocated I don't understand why. My logic beyond the copy constructor is simple: I iterate over the list I'm copying, which is obj, and I add a new element to this list, which is the list I'm copying to. Since my add() function creates a new element with, well, new, I can't see where my two lists share an element which I'm trying to delete twice in the destructor. What am I doing wrong?

Upvotes: 2

Views: 167

Answers (1)

Landstalker
Landstalker

Reputation: 1368

You forgot to initialize your _head in copy constructor:

// Copy constructor
LinkedList(const LinkedList &obj) {

    _head = NULL; // <- Add This

    ListNode *node = obj._head;
    while (node != nullptr) {
        this -> add(node -> val);
        node = node -> next;
    }
}

Upvotes: 6

Related Questions