What is segmentation fault?

Asked 2 years ago, Updated 2 years ago, 115 views

There are 3 things I'm curious about.

c c++ segmentation-fault

2022-09-21 19:16

1 Answers

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.

In an unacceptable way

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!
}


2022-09-21 19:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.