Hartun
Hartun

Reputation: 113

Segmentation Fault in linkedlist and debugging options for Linux

full code: ( https://github.com/martin-varbanov96/fmi_summer_2017/tree/master/daa/bfs )

So I have to do a graph BFS, but first I have to make a fully working linkedList and I have a problem with the segmentation fault.

The real problem here is that I am using Linux and trying to learn vim and so I have no good way of debugging and I have to use gdb.

The general problem is that I have a node struct and I want to assign it a value but it gives a Segmentation fault. Here is the node:

struct node{
        int value;
        node* next;
};

and than in the main function, program crashes in the assigment:

int main(){
        node* b;
        b->value=1;
        linkedList a;
         ...

this is the gdb result:

Wed Mar 29; 15:59:29; marton;~/documents/github/fmi_summer_2017/daa/bfs ;  $  gdb a.out 
GNU gdb (GDB; openSUSE Leap 42.1) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-suse-linux".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://bugs.opensuse.org/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...done.
(gdb) start
Temporary breakpoint 1 at 0x4008f5: file main.cpp, line 5.
Starting program: /home/marton/documents/github/fmi_summer_2017/daa/bfs/a.out 
step
Temporary breakpoint 1, main () at main.cpp:5
5               b->value=1;
(gdb) step

Program received signal SIGSEGV, Segmentation fault.
0x00000000004008f9 in main () at main.cpp:5
5               b->value=1;
(gdb) 

Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
(gdb) 

So my question is what is the problem with my code and what do I need to do to fix it and is there a better way to debug my things in the future?

EDIT:

changes main, as said:

int main(){
        node* b=new node();
        b->value=1;

gdb:

Temporary breakpoint 1, main () at main.cpp:4
4               node* b=new node();
(gdb) step
operator new (sz=16) at ../../../../libstdc++-v3/libsupc++/new_op.cc:43
43      ../../../../libstdc++-v3/libsupc++/new_op.cc: No such file or directory.
(gdb) 
48      in ../../../../libstdc++-v3/libsupc++/new_op.cc
(gdb) 
43      in ../../../../libstdc++-v3/libsupc++/new_op.cc
(gdb) 
48      in ../../../../libstdc++-v3/libsupc++/new_op.cc
(gdb) 
50      in ../../../../libstdc++-v3/libsupc++/new_op.cc
(gdb) 

__GI___libc_malloc (bytes=16) at malloc.c:2897
2897    malloc.c: No such file or directory.
(gdb) 
2902    in malloc.c
(gdb) 
2903    in malloc.c
(gdb) 
2906    in malloc.c
(gdb) 
2908    in malloc.c
(gdb) 
_int_malloc (av=av@entry=0x7ffff753a620 <main_arena>, bytes=bytes@entry=16) at malloc.c:3325
3325    in malloc.c
(gdb) 
3355    in malloc.c
(gdb) 
3359    in malloc.c
(gdb) 
3373    in malloc.c
(gdb) 
3375    in malloc.c

Real problem is how to debug properly with vim and linux

Upvotes: 0

Views: 423

Answers (3)

Fatih Altuntaş
Fatih Altuntaş

Reputation: 406

int main(){
    node* b;
    b->value=1;
    linkedList a;

Problem is pointer b, you declared a pointer type of node, but it points null(nowhere),then you try assign b->value, therefore throw a segmentation fault. You must assign a address your pointer b ,like below,

b=new node;
b->value=1;

then you can assign any value

Upvotes: 0

Godineau F&#233;licie
Godineau F&#233;licie

Reputation: 438

You are using a non allocated structure. malloc (C) or new (C++) your structure before using it

int main(){
    node* b;
    b = malloc(sizeof(node));
    b->value=1;
    linkedList a;
}

int main(){
    node* b = new node();
    b = malloc(sizeof(node));
    b->value=1;
    linkedList a;
}

Upvotes: 0

WarNaX
WarNaX

Reputation: 83

You should initialize b pointer before assign value to it

node *b = new node();
b->value = 1;

Upvotes: 2

Related Questions