0
#include<iostream>
using namespace std;
class list
{
public:
 list();
 bool insertHead(int n);
private:
 struct node
 {
 int item;
 node *next;
 };
 node* head;
 };
list::list()
{
 head = NULL;
 head -> item = 0;
 head -> next = NULL;
}
bool list::insertHead(int n)
{
 node* tempptr = new node;
 tempptr->item = n;
 tempptr->next = head;
 head = tempptr;
 return true;
}
 int main()
 {
 list test1;
 test1.insertHead(4);
 return 0;
 }

This code compiles fine but unfortunately segfaults when running. I tried adding delete tempptr at the end of the insertHead function but to no avail. I am so bad at memory allocation, I know a segmentation fault has to do with memory allocation during runtime. Can anybody help me? I'm just using insertHead to insert a integer into the front of a linked list. Can anybody help me out? Thanks! I combined the implementation and other files together so its easier to read...I think. Thanks

Yu Hao
123k50 gold badges251 silver badges305 bronze badges
asked Jul 18, 2013 at 6:59
4
  • Why would a delete solve a segmentation fault? Also, did you try running your code in a debugger? Commented Jul 18, 2013 at 7:00
  • Where does it segfault? Commented Jul 18, 2013 at 7:00
  • 2
    In list::list you set head = NULL and then you try to dereference it. How do you expect that to work? Commented Jul 18, 2013 at 7:01
  • omg you guys make me look so stupid. Maybe thats an understatement. Thanks so much. I'm smacking myself in the face. I just scratched all the constructor business and replaced it with head = NULL. Thanks! Commented Jul 18, 2013 at 7:16

5 Answers 5

2
 head = NULL;
 head -> item = 0;
*** segmentation fault, beacuse head is null, can't dereference
 head -> next = NULL;
answered Jul 18, 2013 at 7:02

1 Comment

If be pedantic - not necessarily segfault. This is Undefined Behavior, anything could happen.
2

I would suggest using GDB, run gdb with this program. When it segfaults it'll give you a stack trace of exactly where the program seg faulted. you can then print relevant variables using the 'p command'. Seg fault always means accessing memory that is outside of your processes reach (pointers with values that aren't within your process - invalid pointers). Learning to use GDB well will save you a lot of time, it's an insta-fix for seg faults :-)

answered Jul 18, 2013 at 7:03

Comments

1

When creating an empty list, you just need to set head to NULL. There's no need to set its item or next, because it's an empty list. You were dereferencing the NULL pointer, which is not allowed, and on most systems will result in a segmentation fault or similar error.

list::list()
{
 head = NULL;
}
answered Jul 18, 2013 at 7:05

Comments

0

Are you really suprised that this code crashes?

head = NULL;
head -> item = 0;
head -> next = NULL;

You assign NULL to a pointer and immediately reference it.

answered Jul 18, 2013 at 7:02

Comments

0
head = NULL;
head -> item = 0;
head -> next = NULL;

can not possibly work. The -> operator dereferences the pointer, which obviously is not possible if its value is set to NULL. You need to allocate memory for the head beforehand:

head = new node;
answered Jul 18, 2013 at 7:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.