0

As I have just learned templates in C++, I started to practice it. I am at the beginning of the implementation of a Linked List using templates and I have been having an error on Xcode, where it states: "Thread 1: EXC_BAD_ACCESS(code=1, address=0x9)". In my other compiler, it says it is a segmentation fault, but I cannot find the cause of it. What might be the reason of this error?

#include <iostream>

using namespace std;

template <typename T> struct Node{

    T value;
    Node * ptr;
};

template <typename T> class LinkedList {
public:

    LinkedList(const T & element);
    T & push(const T & element);
    T & pop(const int index);
    int get_size() const { return this->_size; }
    void printll();

private:
    LinkedList(){};
    struct Node<T> * head;
    int _size;
};

template <typename T> LinkedList<T>::LinkedList(const T & element){

    struct Node<T> new_node;           
    new_node.value = element;
    new_node.ptr = nullptr;

    this->head = &new_node;            

    this->_size = 1;                    
}

template <typename T> void LinkedList<T>::printll(){

    if(this->head != nullptr){

        Node<T> * temp = this->head;
        while((*temp).ptr != nullptr){  // Error here.
            cout << (*temp).value << " ";
            temp = temp->ptr;
        }
        cout << (*temp).value << endl;
        cout << endl;

    }else {
        cout << "The list is empty!" << endl;
    }
}

int main(int argc, char** argv){

    LinkedList<int> l1(5);

    l1.printll();

    return 0;
}
user2736738
  • 30,591
  • 5
  • 42
  • 56
maufcost
  • 169
  • 11
  • You are requested to correctly tag your question next time. – user2736738 Feb 28 '18 at 14:58
  • Note that semantically calling `push()` and `pop()` your methods makes it look like it's a stack implementation and not a linked list. In a linked list you can delete any element, and you can also insert an element, unlike in stacks where you can only add to the top of it and remove from the top of it. – Iharob Al Asimi Feb 28 '18 at 15:00
  • 2
    The reason is that `new_node` is a local and goes out of scope when your constructor completes. You storing a pointer to it doesn't magically keep it alive, it just means you have an invalid pointer you can never legally dereference. – Useless Feb 28 '18 at 15:03
  • I understand that c++ programmers avoid pointers and stuff, but `(*temp).ptr`??? just write `temp->ptr` and `temp->value` – Iharob Al Asimi Feb 28 '18 at 15:03
  • Yes, as @Useless said. So use `new` instead and create a pointer to persistent memory on the heap, because the memory used for your `new_node` lives in the stack frame of the constructor, and is **popped** from it when the function/method returns. Note that in the end, c++ class methods are roughly just functions with a special and invisible argument. – Iharob Al Asimi Feb 28 '18 at 15:05
  • 1
    This is effectively a duplicate of [this old classic](https://stackoverflow.com/q/6441218/212858). I'm holding off closing as a dupe because there might be a better match, suggestions are welcome. – Useless Feb 28 '18 at 15:06
  • Indeed! That was a silly mistake. I appreciate your quick answers. – maufcost Feb 28 '18 at 15:10
  • @Useless +1 for the link – Wander3r Feb 28 '18 at 15:10

0 Answers0