Segmentation fault.段错误调试

1、定义指针一定要记得初始化NULL

gdb bugging
(gdb) start
(gdb) n
(gdb) l
(gdb) where

[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 <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 "i686-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值