Program received signal SIGSEGV, Segmentation fault.段错误调试

一、可能的原因                                                                                                    

1、指针未初始化

[easie@localhost zxxtest]$ g++ -o bugging -g bugging.cc                                    #编译程序得到可执行文件,这里一定要添加-g才能调试
[easie@localhost zxxtest]$ ./bugging                                                       #运行可执行文件
please input a string:zxx
Segmentation fault (core dumped)                                                           #段错误          
[easie@localhost zxxtest]$ gdb bugging                                                     #用GDB调试
GNU gdb (GDB) Fedora (7.4.50.20120120-54.fc17)
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
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 "i686-redhat-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /home/easie/test/Aqua-Sim-1.0/zxxtest/bugging...done.
(gdb) l                                                                                     #源程序,错误应该是gets函数接收字符串之前string未被初始化
                                                                                            #可用buff为string初始化,为了避免安全性隐患,使用scanf接收
1	#include "cstdlib"                                                                   
2	#include "cstdio"
3	static char buff[256];
4	static char *string;
5	int main()
6	{
7	  for(int i=0;i<256;i++){buff[i]=0;}
8	//string=buff;
9	  printf("please input a string:");
10	  gets(string);
(gdb) r                                                                         #运行程序
Starting program: /home/easie/test/Aqua-Sim-1.0/zxxtest/bugging 
please input a string:zxxProgram received signal SIGSEGV, Segmentation fault. #段错误
_IO_gets (buf=0x0) at iogets.c:5555 buf[0] = (char) ch;
(gdb) where                                                                      #where非常好用,直接找到了出错的地方在第10行
#0 _IO_gets (buf=0x0) at iogets.c:55
#1 0x0804851c in main () at bugging.cc:10
(gdb)list                                                      #可能是指针未初始化,我每次run之后再list就不是原来的源程序了,我还没找到原因
50 /* This is very tricky since a file descriptor may be in the
51 non-blocking mode. The error flag doesn't mean much in this
52 case. We return an error only when there is a new error. 
*/53 int old_error = _IO_stdin->_IO_file_flags & _IO_ERR_SEEN;
54 _IO_stdin->_IO_file_flags &= ~_IO_ERR_SEEN;
55 buf[0] = (char) ch;
56 count = INTUSE(_IO_getline) (_IO_stdin, buf + 1, INT_MAX, '\n', 0) + 1;
57 if (_IO_stdin->_IO_file_flags & _IO_ERR_SEEN)
58 {
59 retval = NULL;
(gdb) list 7
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
GNU(gdb) main 7 signal 
Undefined maintenance command: "7 signal". Try "help maintenance".
(gdb) man 7 signal
Undefined command: "man". Try "help".
(gdb) man 7 signal|grep SEGV
Undefined command: "man". Try "help".
(gdb) Q
A debugging session is active.Inferior 1 [process 17662] will be killed.
Quit anyway? (y or n) Y
[easie@localhost zxxtest]$ man 7 signal|grep SEGV          #通过man 7 signal查看SIGSEGV的信息 signals, such as SIGSEGV and SIGFPE, 
generated as a consequence of exe‐ SIGSEGV 11 Core Invalid memory reference
所以,定义了指针后记得初始化,在使用的时候记得判断是否为NULL。

修改后的程序如下:

1	#include "cstdlib"                                                                   
2	#include "cstdio"
3	static char buff[256];
4	static char *string;
5	int main()
6	{
7	  for(int i=0;i<256;i++){buff[i]=0;}
8	 string=buff;  //初始化string
9	  printf("please input a string:");
10	 // gets(string);
11     scanf("%[^\n]",string);//获取屏幕输入的字符串

2、在使用数组的时候是否被初始化,数组下标是否越界,数组元素是否存在等
例如:

#include 
int
main()
{
char test[1];
printf(”%c”, test[1000000000]);
return 0;
}

3、在变量处理的时候变量的格式控制是否合理等
例如:

#include 
int
main()
{
int b = 10;
printf(”%s/n”, b);
return 0;
}
比如,试图把char型或者是int的按照%s输出或存放起来,类似的,还有诸如:sprintf等的格式控制问题

#include 
#include 
char c=’c';
int i=10;
char buf[100];
printf(”%s”, c); //试图把char型按照字符串格式输出
printf(”%s”, i); //试图把int型按照字符串输出
memset(buf, 0, 100);
sprintf(buf, “%s”, c); //试图把char型按照字符串格式转换
memset(buf, 0, 100);
sprintf(buf, “%s”, i); //试图把int型按照字符串转换
二、调试方法
用GDB调试,参考第一条即可,运用r,where,list等命令

参考文章:http://blog.csdn.net/love_gaohz/article/details/6597857







你可能感兴趣的:(Linux系统学习,linux,gdb,调试)