C语言 segment fault

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于C语言 segment fault的主要内容,如果未能解决你的问题,请参考以下文章

关于warning no stack segment的问题 - 16位汇编语言代码问题

R语言segments函数绘制两点之间的线段实战

c_cpp ST_Segmentize.c

Python调用C的SDK出现返回值不符合预期以及Segmentation fault

运行时的C程序

Core Dump (Segmentation fault) in C/C++