C语言 segment fault

Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.” It’s a helper mechanism that keeps you from corrupting the memory and introducing hard-to-debug memory bugs. Whenever you get a segfault you know you are doing something wrong with memory – accessing variable that has already been freed, writing to a read-only portion of the memory, etc. Segmentation fault is essentially the same in most languages that let you mess with the memory management, there is no principial difference between segfaults in C and C++.

There are many ways to get a segfault, at least in the lower-level languages such as C(++). A common way to get a segfault is to dereference a null pointer:

以上文字转自:http://stackoverflow.com/questions/2346806/what-is-segmentation-fault

int main(){

char* str = "Foo";

*str = ‘a‘;

}

int main(){

char* str = NULL;

*str = ‘a‘;

}

以上两个程序会报segment fault

时间: 2024-08-09 21:59:04

C语言 segment fault的相关文章

利用linux信号机制调试段错误(Segment fault)

在实际开发过程中,大家可能会遇到段错误的问题,虽然是个老问题,但是其带来的隐患是极大的,只要出现一次,程序立即崩溃中止.如果程序运行在PC中,segment fault的调试相对比较方便,因为可以通过串口.显示器可以查看消息,只要程序运行,通过GDB调试工具即可捕捉产生segment fault的具体原因.但是不知大家有没有想法,当程序运行在嵌入式设备上时,你所面临资源的缺乏,你没有串口打印信息,没有显示器可查看,你不知道程序运行的状态,如果程序的产生segment falut这种bug发生的周

出现segment fault 错误的几种原因

segment fault 段异常各种原因www.MyException.Cn 发布于:2012-11-26 11:48:50 浏览:24次 0 segment fault 段错误各种原因一 造成segment fault,产生core dump的可能原因1.内存访问越界 a) 由于使用错误的下标,导致数组访问越界 b) 搜索字符串时,依靠字符串结束符来判断字符串是否结束,但是字符串没有正常的使用结束符 c) 使用strcpy, strcat, sprintf, strcmp, strcasec

Segment fault及LINUX core dump详解

源自:http://andyniu.iteye.com/blog/1965571 core dump的概念: A core dump is the recorded state of the working memory of a computer program at a specific time, generally when the program has terminated abnormally (crashed). In practice, other key pieces of

Segment fault及LINUX core dump详解 (zz)

C 程序在进行中发生segment fault(core dump)错误,通常与内存操作不当有关,主要有以下几种情况: (1)数组越界. (2)修改了只读内存. (3)scanf("%d",n),n不是指针. …… 1. 前言: 有的程序可以通过编译, 但在运行时会出现Segment fault(段错误). 这通常都是指针错误引起的. 但这不像编译错误一样会提示到文件->行, 而是没有任何信息, 使得我们的调试变得困难起来. 2. gdb: 有一种办法是, 我们用gdb的step

一次php脚本出现段错误(Segment fault)的经历

今天在一台新服务器上cli运行一个php脚本,出现了Segment fault错误,第一感觉应该是某个扩展安装有问题 这段php代码是调用soap接口,查看soap扩展没啥问题,一时不知道是啥原因. 想到可以用gdb调试core文件,于是去安装了一下gdb  ,并执行以下命令:ulimit -c unlimited 再次执行果然产生了core.PID文件,用gdb php路径 core.PID提示没有调试信息. 这.... 问了三金锅,原来php编译的时候需要带上 --debug才行. 于是重新

段错误Segment Fault定位,即core dump文件与gdb定位

使用C++开发系统有时会出现段错误,即Segment Fault.此类错误程序直接崩溃,通常没有任何有用信息输出,很难定位bug,因而无从解决问题.今天我们介绍core dump文件,并使用gdb进行调试,以此来定位段错误问题.此文同时用以备忘. 一.core dump Core dump也称核心转储, 当程序运行过程中异常退出时, 由操作系统把程序当前的内存状况存储在一个core文件中, 称之为core dump文件. 系统默认不生成core dump文件,可以使用ulimit命令进行查看和设

【Z】段错误Segment Fault定位,即core dump文件与gdb定位

使用C++开发系统有时会出现段错误,即Segment Fault.此类错误程序直接崩溃,通常没有任何有用信息输出,很难定位bug,因而无从解决问题.今天我们介绍core dump文件,并使用gdb进行调试,以此来定位段错误问题.此文同时用以备忘. 一.core dump Core dump也称核心转储, 当程序运行过程中异常退出时, 由操作系统把程序当前的内存状况存储在一个core文件中, 称之为core dump文件. 系统默认不生成core dump文件,可以使用ulimit命令进行查看和设

关于Release下没有问题,Debug下出现Segment Fault

前言: 在项目开发中出现Release下跑程序没有问题,Debug下出现Segment Fault. 代码如下: void fun(int lines){ int* pA; if(pA==nullptr){ pA=new int[lines]; } memset(pA,0,sizeof(int)*lines); //Segment Fault } 原因: Debug下局部变量中指针没有初始化,而编译器给了pA一个随机值,导致在if判断的时候跳出语句块,没有成功new内存,而在后面进行memset

Segment Fault 的访问地址究竟在哪里?

如果指针写数据失败, 比如 *p = 1 可以进一步看, 究竟为何失败 可以用察看  /proc/{pid}/maps, 但往往这个时候,程序已经crash 了,就找不到这个文件. 那就只能在crash 之前code 插入代码看了. 写一个copyfile 的函数 static void copyfile(char * rfile, char * wfile) { int rfd, wfd,size; rfd=open(rfile,O_RDONLY); wfd=open(wfile,O_WRON