whitebumblebee
whitebumblebee

Reputation: 79

How to fix "Segmentation fault(core dump)" in C++?

I am trying to create Linked list in c++ using class. I used only two methods for it that is pushFront() and traverse() to push the new element in the head of the linked list and display the elements in the list at any given time. Whenever i use traverse method i am getting the error "Segmentation fault"

My code is shown below. I looked up about this error in this site and elsewhere. Segmentation fault is said to occur when we attempt to access variable which is not ours to take. i.e it is is space which is not accesible to us. But as one can see in my code I am attempting to use head and tail the private members of the class. But i am using them in the methods of the class themselves. So i should be allowed to do that right? I don't know where am i going wrong with this.

#include <iostream>
using namespace std;
struct Node
{
    int data;
    Node * next;
};
class LinkedList
{
    private:
        Node * head;
        Node * tail;
    public:
        LinkedList();
        void pushFront(int i);
        void traverse();
};
LinkedList::LinkedList()
{
    Node * head = NULL;
    Node * tail = NULL;
}
void LinkedList::pushFront(int i)
{
    Node * newNode =new Node;
    newNode->data=i;
    newNode->next=head;
    head=newNode;
    if(tail==NULL)
        tail = head;
}
void LinkedList::traverse()
{
    if (head==NULL){
        cout<<"empty list. add elements";
        return;
    }
    Node * ptr =  head;
    while(ptr!=NULL)
    {
        cout<<ptr->data;
        ptr=ptr->next;
    }
}
int main()
{
    LinkedList l;
    l.pushFront(10);
    l.pushFront(9);
    l.traverse();
    return 0;
}

I expect that output should be 910 as should be printed by traverse. But i get the segmentation fault error. Can anyone point out where i am wrong?

Upvotes: 0

Views: 579

Answers (1)

bruno
bruno

Reputation: 32596

LinkedList::LinkedList()
{
  Node * head = NULL;
  Node * tail = NULL;
}

must be (minimal change) :

LinkedList::LinkedList()
{
  head = NULL;
  tail = NULL;
}

else you do not initialize the right attributes but local variables


I encourage you to compile to produce all the warnings, if I compile your code with g++ -Wall -pedantic c.cc this gives

c.cc: In constructor 'LinkedList::LinkedList()':
c.cc:20: warning: unused variable 'head'
c.cc:21: warning: unused variable 'tail'

and this is exactly where the problem is


and about valgrind, if I execute under it that gives :

valgrind ./a.out
==18508== Memcheck, a memory error detector
==18508== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==18508== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==18508== Command: ./a.out
==18508== 
==18508== Conditional jump or move depends on uninitialised value(s)
==18508==    at 0x400829: LinkedList::pushFront(int) (c.cc:29)
==18508==    by 0x4008C2: main (c.cc:48)
==18508== 
==18508== Conditional jump or move depends on uninitialised value(s)
==18508==    at 0x40089A: LinkedList::traverse() (c.cc:39)
==18508==    by 0x4008DF: main (c.cc:50)
==18508== 
==18508== Use of uninitialised value of size 8
==18508==    at 0x400876: LinkedList::traverse() (c.cc:41)
==18508==    by 0x4008DF: main (c.cc:50)
==18508== 
==18508== Use of uninitialised value of size 8
==18508==    at 0x400888: LinkedList::traverse() (c.cc:42)
==18508==    by 0x4008DF: main (c.cc:50)
==18508== 
==18508== Invalid read of size 4
==18508==    at 0x400876: LinkedList::traverse() (c.cc:41)
==18508==    by 0x4008DF: main (c.cc:50)
==18508==  Address 0x4b43415254475542 is not stack'd, malloc'd or (recently) free'd
==18508== 
==18508== 
==18508== Process terminating with default action of signal 11 (SIGSEGV)
==18508==  General Protection Fault
==18508==    at 0x400876: LinkedList::traverse() (c.cc:41)
==18508==    by 0x4008DF: main (c.cc:50)
9101778121006==18508== 
==18508== HEAP SUMMARY:
==18508==     in use at exit: 32 bytes in 2 blocks
==18508==   total heap usage: 2 allocs, 0 frees, 32 bytes allocated
==18508== 
==18508== LEAK SUMMARY:
==18508==    definitely lost: 0 bytes in 0 blocks
==18508==    indirectly lost: 0 bytes in 0 blocks
==18508==      possibly lost: 0 bytes in 0 blocks
==18508==    still reachable: 32 bytes in 2 blocks
==18508==         suppressed: 0 bytes in 0 blocks
==18508== Rerun with --leak-check=full to see details of leaked memory
==18508== 
==18508== For counts of detected and suppressed errors, rerun with: -v
==18508== Use --track-origins=yes to see where uninitialised values come from
==18508== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 6 from 6)
Segmentation fault

so you see also the head and tail are not initialized etc

Upvotes: 5

Related Questions