如果程序运行时出现段错误,用gdb可以很容易定位到究竟是哪一行引发的段错误,例如这个小程序:
段错误调试实例一:
#include <stdio.h>
int main(void)
{
int man = 0;
scanf("%d", man);
return 0;
}
[zhangsan@localhost study-c]$ gcc -g gdb4.c -o gdb4
[zhangsan@localhost study-c]$ ./gdb4
dsd
[zhangsan@localhost study-c]$ ./gdb4
123
Segmentation fault (core dumped)
调试过程如下:
[zhangsan@localhost study-c]$ gdb gdb4
GNU gdb (GDB) 7.6.1
Copyright (C) 2013 Free Software Foundation, Inc.
...
Reading symbols from /home/zhangsan/study-c/gdb4...done.
(gdb) run
Starting program: /home/zhangsan/study-c/gdb4
1234
Program received signal SIGSEGV, Segmentation fault.
0x0000003d6b056ac0 in _IO_vfscanf_internal () from /lib64/libc.so.6
(gdb) bt
#0 0x0000003d6b056ac0 in _IO_vfscanf_internal () from /lib64/libc.so.6
#1 0x0000003d6b0642bd in