segmentation fault
occurs when accessing memory in an unauthorized manner and when accessing an unauthorized memory area
It prevents users from contaminating memory and informs them of memory bugs that are difficult to debug.
There is no special difference between the sigfaults of C and C++.++. In fact, there is no difference in segmentation fault in most languages.
In low-level programming languages such as C++, segfault
occurs a lot.
And it's usually associated with a pointer.
int main(){
int* myptr = NULL;
*myptr = 3;
}
int main(){
char* myptr = "hello! world!";
*myptr = 'h';
}
int main(){
char* myptr = NULL;
{
char c = '3';
myptr = &c;
}
//block is over, so the lifetime of c is over, but myptr still points to c
*myptr = 'G'; //where segfault!
}
© 2024 OneMinuteCode. All rights reserved.