Program received signal SIGSEGV, Segmentation fault.段错误调试
来源:互联网 发布:数据库水平切分 编辑:程序博客网 时间:2024/05/16 11:02
一、可能的原因
1、指针未初始化
[easie@localhost zxxtest]$ g++ -o bugging -g bugging.cc #编译程序得到可执行文件,这里一定要添加-g才能调试[easie@localhost zxxtest]$ ./bugging #运行可执行文件please input a string:zxxSegmentation 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"3static char buff[256];4static char *string;5int 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 the51 non-blocking mode. The error flag doesn't mean much in this52 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 72 This file is part of the GNU C Library.34 The GNU C Library is free software; you can redistribute it and/or5 modify it under the terms of the GNU Lesser General Public6 License as published by the Free Software Foundation; either7 version 2.1 of the License, or (at your option) any later version.89 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 signalUndefined command: "man". Try "help".(gdb) man 7 signal|grep SEGVUndefined command: "man". Try "help".(gdb) QA 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"3static char buff[256];4static char *string;5int main()6{7 for(int i=0;i<256;i++){buff[i]=0;}8 string=buff; //初始化string9 printf("please input a string:");10 // gets(string);11 scanf("%[^\n]",string);//获取屏幕输入的字符串
2、在使用数组的时候是否被初始化,数组下标是否越界,数组元素是否存在等
例如:
#include <stdio.h>intmain(){char test[1];printf(”%c”, test[1000000000]);return 0;}
3、在变量处理的时候变量的格式控制是否合理等
例如:
#include <stdio.h>intmain(){int b = 10;printf(”%s/n”, b);return 0;}比如,试图把char型或者是int的按照%s输出或存放起来,类似的,还有诸如:sprintf等的格式控制问题
#include <stdio.h>#include <string.h>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
阅读全文
0 0
- Program received signal SIGSEGV, Segmentation fault.段错误调试
- Program received signal SIGSEGV, Segmentation fault.
- Program received signal SIGSEGV, Segmentation fault.
- Program received signal SIGSEGV, Segmentation fault.
- Program received signal SIGSEGV, Segmentation fault
- program received signal SIGSEGV, Segmentation fault
- debug information: Program received signal SIGSEGV,segmentation fault.
- Program received signal SIGSEGV, Segmentation fault.(转)
- 解决Program received signal SIGSEGV, Segmentation fault.的辛酸
- error: Program received signal SIGSEGV, Segmentation fault. (Codeblocks, C++)
- error: Program received signal SIGSEGV, Segmentation fault. (Codeblocks, C++)(2)
- 错误总结:C/C++运行时提示".exe已停止工作"? 调试时出现Program received signal SIGSEGV,Segmentation fault?引用无效内存一般是什么错误?
- Program received signal SIGSEGV, Segmentation faul;
- Ubuntu+GCC(g++):Program received signal SIGSEGV, Segmentation fault
- mingw 编译c++ 最简单的程序出现Program received signal SIGSEGV, Segmentation fault.
- Process returned -1073741571 (0xC00000FD) Program received signal SIGSEGV, Segmentation fault.
- 段错误(segmentation fault)总结及调试
- Segmentation fault段错误调试总结
- PHP 计算两个文件的相对路径
- GMSSL在windows下使用vs2010编译
- linux下mysql5.7.19数据库备份(全量备份、增量备份)
- 设计模式--工厂方法模式
- java 反射
- Program received signal SIGSEGV, Segmentation fault.段错误调试
- 在CentOS上基于Apache http服务搭建git远程仓库(一)
- Python的多种风格快速排序案例
- Java中equals()与hashCode()方法详解
- Caused by: java.lang.RuntimeException: Could not generate DH keypair
- Python3+Django+Mysql配置
- ORACLE数据库使用SYS用户登录出现:ORA-28009:connection as SYS should be as SYSDBA or SYSOPER报错处理办法
- linxu C 学习笔记(三):标准I/O库
- MySQL数据备份之mysqldump使用