2

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?

10
  • 1
    The code looks okay to me. Can you use a debugger to see which line of code the error comes from? (on Linux, put gdb in front of your program when you run it, and when it prints an error and pauses, type bt to see the call stack)
    – user253751
    Mar 12, 2020 at 11:38
  • You code is working fine in VS2017, no issue, I am able to run it without any issue. Try to clean solution and rebuild it and check.
    – Sanjeev
    Mar 12, 2020 at 11:39
  • The code looks fine to me so far, are you sure the code you run is the code you compile? Otherwise use your debugger. If you don't know how to use it, it's time to start learning. Mar 12, 2020 at 11:40
  • @user253751 I'm using CLion on Mac OS X. I asked a friend who also uses CLion + Mac OS X (her OS version is newer, though) and she said she got the same error. Mar 12, 2020 at 11:51
  • 1
    Not the issue in this program, but don't forget the Rule Of Three: Give LinkedList an operator=(const LinkedList&). Then you might optionally improve the class by following the Rule Of Five.
    – aschepler
    Mar 12, 2020 at 12:06

1 Answer 1

6

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;
    }
}
3
  • 3
    LinkedList(const LinkedList &obj) : _head(nullptr) { ...
    – Ted Lyngmo
    Mar 12, 2020 at 12:02
  • @TedLyngmo I'm from old school ... ^^ Mar 12, 2020 at 12:03
  • 1
    @TedLyngmo So, I'm from a very very old school ! ^^ Mar 12, 2020 at 13:42

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.