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