在写程序的时候,最容易遇到的一类问题基本上就是段错误的问题,可以借助gdb来解决这类问题,下面是个例子:
int main(void) { int man = 0; scanf("%d", man); return 0; }运行输入会出现段错误:
root@wc:~/Codes/Learn/learnGDB# ./segfault 1 段错误 (核心已转储)下面开始调试:
(gdb) start Temporary breakpoint 1 at 0x40051e: file segfault.c, line 11. Starting program: /root/Codes/Learn/learnGDB/segfault Temporary breakpoint 1, main () at segfault.c:11 11 int man = 0; (gdb) n 12 scanf("%d", man); (gdb) n 123 Program received signal SIGSEGV, Segmentation fault. 0x00007ffff7a6c742 in _IO_vfscanf_internal (s=<optimized out>, format=<optimized out>, argptr=argptr@entry=0x7fffffffdf28, errp=errp@entry=0x0) at vfscanf.c:1857 1857 vfscanf.c: 没有那个文件或目录.从上面可以看出,段错误的原因是scanf函数调用产生了一些问题,回到源文件查看会发现scanf的里面少加了一个&符号,所以导致了段错误。
#include<stdio.h> int main(void) { int sum = 0, i = 0; char input[5];scanf("%s", input); for (i = 0; input[i] != '\0'; i++) { if (input[i] < '0' || input[i] > '9') { printf("Invalid input!\n"); sum = -1; break; } sum = sum*10 + input[i] - '0'; } printf("input=%d\n", sum); return 0; }这个程序输入一个较长的字符串的时候就会出现段错误:
root@wc:~/Codes/Learn/learnGDB# ./segfault1 1234567890987654321234567890987654323456789098765432345678 Invalid input! input=-1 段错误 (核心已转储)使用gdb调试查看:
(gdb) n 12345678987654321234567898765432123456789876543212345678987654321 13 for (i = 0; input[i] != '\0'; i++) { (gdb) c Continuing. Invalid input! input=-1 Program received signal SIGSEGV, Segmentation fault. 0x0000000000400659 in main () at segfault1.c:25 25 } (gdb) l 20 sum = sum*10 + input[i] - '0'; 21 22 } 23 printf("input=%d\n", sum); 24 return 0; 25 } (gdb) n 12345678987654321234567898765432123456789876543212345678987654321 13 for (i = 0; input[i] != '\0'; i++) { (gdb) c Continuing. Invalid input! input=-1 Program received signal SIGSEGV, Segmentation fault. 0x0000000000400659 in main () at segfault1.c:25 25 } (gdb) l 20 sum = sum*10 + input[i] - '0'; 21 22 } 23 printf("input=%d\n", sum); 24 return 0; 25 }程序显示段错误发生在25行,但是这一行什么都没有,这可以算是一条规律,如果某个函数中发生访问越界,很可能并不立即产生段错误,而在函数返回时却产生段错误。这里实际上还是scanf函数存在问题。