码迷,mamicode.com
首页 > 编程语言 > 详细

C语言 segment fault

时间:2016-01-25 19:12:26      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/weplaysoft/p/Segment_fault.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!