CPP- I am getting Segmentation fault (core dumped)?

That’s why there are debug tools like gdb (just google for it 😉 ).

This is the backtrace:

#0  0x00000000004009ba in node::setnext (this=0x0, x=0x614cc0) at a.cpp:25
#1  0x0000000000400c18 in list::insertend (this=0x7fffffffdf10, x=45) at a.cpp:109
#2  0x00000000004008df in main () at a.cpp:137

that means in line 137 there is a function call (l.insertend(45)), then in line 109, there is the next function call (temp1->setnext(temp2)) and the segfault occurs on line 25 (next = x). This is because the node is not initialised (temp1 is 0 in line 109).

Your while-loop is the problem, if you change is to something like this:

while (temp1->getnext() != 0)
    temp1 = temp1->getnext();

This will solve your first issue, but you will get another segfault 😉
Try to solve it on your own with the tools provided. If you still need help, please leave a comment and I will post the answer.

Leave a Comment