完整代码:(https://github.com/martin-varbanov96/fmi_summer_2017/tree/master/daa/bfs)
所以我必须做一个图表BFS,但首先我必须制作一个完全正常工作的linkList,我遇到了分段错误的问题。
这里真正的问题是我正在使用Linux并尝试学习vim,所以我没有好的调试方法,我必须使用gdb。
一般的问题是我有一个节点结构,我想为它分配一个值,但它给出了一个Segmentation错误。这是节点:
struct node{
int value;
node* next;
};
并且在主要功能中,程序在分配中崩溃:
int main(){
node* b;
b->value=1;
linkedList a;
...
这是gdb的结果:
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)
所以我的问题是我的代码有什么问题,我需要做些什么来修复它?是否有更好的方法来调试我的东西?
编辑:
改变主要,如上所述:
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
真正的问题是如何使用vim和linux正确调试
答案 0 :(得分:2)
您应该在为其指定值之前初始化b指针
node *b = new node();
b->value = 1;
答案 1 :(得分:0)
您正在使用未分配的结构。 malloc(C)或new(C ++)使用它之前的结构
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;
}
答案 2 :(得分:0)
int main(){
node* b;
b->value=1;
linkedList a;
问题是指针b,你声明了节点的指针类型,但它指向null(无处),然后你尝试赋值b-&gt;值,因此抛出一个分段错误。 您必须为指针b指定一个地址,如下所示,
b=new node;
b->value=1;
然后你可以分配任何值