0

Why does this cause a SegFault Error? I've tried to run a backtrace with gdb, but it has given me no help. Any help would be appreciated, I've been pulling my hair out over this for hours.

my node.h

#ifndef NODE_H
#define NODE_H

#include <string>
using namespace std;

class Node
{
  public:

    Node(const string, const int) ;
   ~Node() { }
    void setNext(Node *);//setter for the next variable
    Node * getNext();// getter for the next variable
    string getKey();// getter for the key variable
    int getDistance();   // getter for the dist variable

  private:
    Node *next;
    int dist;
    string key;
};

#endif

My Node.cpp

#include "node.h"
#include <string>

Node::Node(string k, int d){
    key = k;
    dist = d;
}

void Node::setNext(Node * n){
    next = n;
}

Node * Node::getNext(){
    return next;
}

string Node::getKey(){
return key;
}

int Node::getDistance(){
    return dist;
}

My list.h

#ifndef LIST_H
#define LIST_H

#include "node.h"

class SLL
{
    public:
        SLL();
        ~SLL() { }
               void Insert (string searchKey, int distance);
               bool Delete (string searchKey);
               void Print();
               int Search(string searchKey);

    private:
        int count;
        Node *head;
    Node *iterator;
    Node *temp;
};

#endif

my List.cpp

#include "list.h"
#include <iostream>

SLL::SLL():head(0){}

void SLL::Insert(string searchKey, int distance){
Node * temp = new Node(searchKey, distance);

if(head == 0){
    head = temp;
}
else{
    temp->setNext(head);
    head = temp;
}
}

bool SLL::Delete(string searchKey){
 if(head == 0){
cout << "An attempt was made to delete a node from an empty list" << endl;
 }
 else{
Node* iterator = head;
Node* last = 0;

while(iterator != 0){
   if (iterator->getKey() == searchKey){
    break;
   }
   else{
    last = iterator;
    iterator = iterator->getNext();
   }
}
if (iterator == 0){
    return false;
}
else{
    if(head == iterator){
        head = head->getNext();

    }
    else {
        last->setNext(iterator->getNext());
    }
    delete iterator;



    }

    }
 }

void SLL:: Print(){
iterator = head;
while(iterator != 0){   
    cout << iterator->getKey()  << "-" << iterator->getDistance() << endl;
    iterator = iterator->getNext();
 }

}

int SLL::Search(string searchKey){

 }

My main.cpp

#include "list.h"
#include "node.h"
#include <iostream>

using namespace std;

int main(int argc, char* argv[]) {
    SLL * sll; 

    sll->Insert("test", 1);
    sll->Insert("test2", 2);
    sll->Delete("test");
    sll->Print();
}
  • 2
    gdb has given you no help?! It should tell you where you get the segfault, and you should tell us. – us2012 Feb 15 '13 at 21:54
  • 1
    What do you think the initial value of sll is? – andre Feb 15 '13 at 22:00
  • Besides the bug causing your segfault which has been answered, you have a few other problems: 1. Take a look at your ctor for Node. You seem to rely on the fact that the last element on the list's next field is 0, but are you ever setting it to 0? 2. Avoid having using statements in your header files, at least on a global scope. This is a Wasp's nest waiting to happen (probably not in a HW assignment but its a good practice to hold). See http://stackoverflow.com/questions/4872373/why-is-including-using-namespace-into-a-header-file-a-bad-idea-in-c – eladidan Feb 15 '13 at 22:16

2 Answers2

3

Hint: Segfault happens here:

int main(int argc, char* argv[]) {
    SLL * sll; 

    sll->Insert("test", 1); // BIG segfault here.
    ...

(No full answers as this looks like homework.)

us2012
  • 16,083
  • 3
  • 46
  • 62
1

In your main function, the pointer to SSL is not initialised, but you dereference it. This is undefined behaviour. In your particular case, this is causing a segmentation violation. Try changing you code to create a SSL object, either on the stack:

int main(int argc, char* argv[]) {
    SLL sll;

    sll.Insert("test", 1);
    // ...
}

or the heap:

int main(int argc, char* argv[]) {
    SLL * sll = new SLL();

    sll->Insert("test", 1);
    // ...
}

BTW, you are never using the temp, iterator, ... fields of the SLL class, never initialise them. In your implementation, you define local variables that hide them, so I'd suggest removing the fields or initialising them in the constructor.

Sylvain Defresne
  • 42,429
  • 12
  • 75
  • 85