C++ segmentation falut <core dumped>

Hi, I'm a beginners of C++, and I've found something weird error called Segmentation falut <core dumped>. I can't understand why my codes keep making this error. Please help me.

*Whole concept is to put certain number of coals in each carriages of Train.
I put the data in array into nodes and each node are linked by Linkedlist.

//node.h


#ifndef JOHN_NODE_H
#define JOHN_NODE_H
#include <cstdlib>

namespace oh_john_5
{
class node
{
public:
typedef int value_type;
node();
void set_data(value_type& new_data);
void set_link(node* new_link);
value_type get_data() const;
node* get_link();

private:

int data_field;
node* link_field;
};
}

#endif

//node.cpp

#include "node.h"

namespace oh_john_5{
node::node()
{
data_field = 0;
link_field = NULL;
}


void node::set_data(value_type& new_data)
{
data_field = new_data;
}

void node::set_link(node* new_link)
{
link_field = new_link;
}

node::value_type node::get_data() const
{
return data_field;
}

node* node::get_link()
{
return link_field;
}
}

//LinkedList.h

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "node.h"
#include <cstdlib>

using oh_john_5::node;
namespace john_linked_5{
class LinkedList
{
public:
LinkedList();
~LinkedList();

void list_head_insert(node*, node::value_type);

void list_insert(node*, node::value_type);


node* get_head_pointer();
node* get_tail_pointer();

private:
node* head_pointer;
node* tail_pointer;
};
}

#endif

//LinkedList.cpp

#include "LinkedList.h"
#include <cstdlib>

namespace john_linked_5{
LinkedList::LinkedList()
{
head_pointer ->set_link(NULL);

tail_pointer ->set_link(NULL);

}

LinkedList::~LinkedList()
{
delete head_pointer;
delete tail_pointer;
}

void LinkedList::list_head_insert(node* head_ptr, node::value_type entry)
{

head_ptr = new node();
head_ptr -> set_data(entry);
head_ptr -> set_link(tail_pointer);
head_pointer -> set_link(head_ptr);
tail_pointer = head_ptr;
}

void LinkedList::list_insert(node* previous_ptr, node::value_type entry)
{
node* insert_ptr;
insert_ptr = new node();

insert_ptr -> set_data(entry);
insert_ptr -> set_link(previous_ptr->get_link());
previous_ptr -> set_link(insert_ptr);

}


node* LinkedList::get_head_pointer()
{
return head_pointer;
}

node* LinkedList::get_tail_pointer()
{
return tail_pointer;
}


}


//Train.h

#ifndef JOHN_TRAIN_H
#define JOHN_TRAIN_H
#include "LinkedList.h"
#include "node.h"
#include <cstdlib>
#include <iostream>
using john_linked_5::LinkedList;
using oh_john_5::node;

namespace john_train_2A{

class Train
{
public:

typedef std::size_t size_type;


Train();
~Train();
void set_train(node::value_type* target);


void insert_new(node::value_type);

friend std::ostream& operator <<(std::ostream&, Train&);
private:

size_type carriageNo;
node::value_type arr[];

LinkedList linking;
};
}
#endif

//Train.cpp

#include "Train.h"

namespace john_train_2A{


std::ostream& operator <<(std::ostream& output, Train& source)
{
node *ptr;
int l = 0;
for( ptr = source.linking.get_head_pointer(); ptr != NULL; ptr = ptr->get_link()){
output<<l+1<<" carriage: "<<ptr->get_data()<<std::endl;
l++;
}
return output;
}

Train::Train(){

}
Train::~Train(){

}

void Train::set_train(node::value_type* target)
{
linking.list_head_insert(linking.get_head_pointer(), arr[0]);

for(int k = 1;k<sizeof(arr)/4;k++){
linking.list_insert(linking.get_tail_pointer(), arr[k]);

}
}
void Train::insert_new(node::value_type entry)
{
linking.list_insert(linking.get_tail_pointer(), entry);
}


}

//demoTrain.cpp

#include <iostream>
#include <cstdlib>
#include "Train.h"
using namespace std;
using john_train_2A::Train;

int main()
{

int a[] = {1500,3200,4500,1000,0,2000,0,2300};
int b[] = {300,5000,450,0,0,2900};
int c[] = {0};

Train* t1;
t1->set_train(a);
Train* t2;
t2->set_train(b);
Train* temp;
temp->set_train(c);

cout<<"Number Of Carraige"<<endl;
cout<<t1<<endl;
cout<<t2<<endl;

system("pause");
return 0;

}
[ code ] [ /code ] tags. Simply googling would have told you that a segmentation fault occurs when you attempt to access memory that doesn't belong to your program, for example
1
2
3
4
bool a[4];
a[4]=true;//seg fault - maximum legal index of a is 3
int *b=0;
*b = 10; // seg fault, can not dereference null pointer 


You are probably dereferencing an uninitialized pointer or attempting to access an array index that is out of bounds somewhere. Seeing as you have a linked list there, probably the former. Best way to figure out what's wrong would be learning how to use a debugger.
Last edited on
Using the mighty gdb, I find this:

1
2
#0  0x0804880b in john_linked_5::LinkedList::list_head_insert (
    this=0xb7e9bff8, head_ptr=0xb7ebc2c0, entry=-1209285952) at 04.cpp:117


Which is talking about the values passed to the function

void LinkedList::list_head_insert(node* head_ptr, node::value_type entry)
entry has the value -1209285952 so it looks to me like you're passing in a bad value from the calling line, which is this line:

linking.list_head_insert(linking.get_head_pointer(), arr[0]);

arr[0] has not been set to anything sensible.
Last edited on
Topic archived. No new replies allowed.