0

I have written this code for a generic linked list:

#include <iostream>
#include <stdio.h>


template <class T> 
struct Node{ 
    T data; 
    Node<T> *next; 
};


template <class T> 
class LinkedList{ 


    Node<T>* head; 
    int size; 

    public: 

        LinkedList<T>() {

            size = 0;
            head = NULL;
        }


        void ll_push(T data){ 
            Node<T>* new_node; 
            new_node->data = data; 
            new_node->next = head; 
            head = new_node; 
            size ++; 
        }

        int ll_size(){ 
            return size; 
        }

        void ll_print_int(){
            Node<T> *start = head; 
            while (start != NULL){ 
                cout<< start->data;
                start = start->next;
                cout<< ","; 
            }
        }


};



int main(){ 

    LinkedList<int> ll;

    for(int i = 0; i < 10; i++){ 
        ll.ll_push(i);
    }

    ll.ll_print_int();

    return 0;
}

But whenever I try to execute ll_print, I get a segmentation fault error. I think I might be misusing pointers at some point but I'm unsure. I'm still a novice in C++, so I would really appreciate if someone could explain what did I do wrong.

4
  • Thank you but this doesn't solve it Commented Oct 22, 2017 at 4:19
  • Any self-respecting compiler is going to issue a diagnostic message because of this common programming mistake, and it's very likely that your compiler did so, but you ignore it because it did produce a compiled program. Well, you just learned a very valuable lesson: don't ignore warnings and other diagnostic messages from your compiler, even if it succeeds in producing a compiled program. There is a reason why your compiler is barking at you, and it's not because your compiler likes to generate long, confusing messages. Commented Oct 22, 2017 at 4:19
  • @Robot0110 I'm still a novice in C++ -- Yet you're trying to write a program that only an intermediate to advanced programmer would be able to write correctly, and even an intermediate programmer may miss some important details. Commented Oct 22, 2017 at 4:25
  • It doesn't solve it because the shown code is not real code, but fantasy code. It fails to compile because the shown code is missing a few obvious statements. It refers to cout, but there's no using namespace std; in sight. Besides this, who knows what other chunks of the real code were left out, and are not shown. It probably has other common bugs, but since the real code is not shown, a real answer won't be possible. Commented Oct 22, 2017 at 4:26

1 Answer 1

1

But whenever I try to execute ll_print, I get a segmentation fault error.

You are using unintialized pointer right here:

Node<T>* new_node;       // unintialized pointer.
new_node->data = data;

Most likely you want:

Node<T>* new_node = new Node<T>;
new_node = ...
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your reply, but this doesn't solve the problem.
@Robot0110 Yes, it does. Did you rebuild your program?
It doesn't solve it because the shown code in the question is not real code, but fantasy code, because it fails to compile, due to an obvious bug. Who knows what the original code actually reads. It probably has other, similar bugs.
This is the full code. And the name state for the cout was left out by mistake. I'm sorry! Thank you, I rebuilt my code and it works now.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.