What causes a SIGSEGV

后端 未结 7 945
野性不改
野性不改 2020-11-27 15:52

I need to know the root cause of the segmentation fault (SIGSEGV), and how to handle it.

7条回答
  •  Happy的楠姐
    2020-11-27 16:42

    Wikipedia has the answer, along with a number of other sources.

    A segfault basically means you did something bad with pointers. This is probably a segfault:

    char *c = NULL;
    ...
    *c; // dereferencing a NULL pointer
    

    Or this:

    char *c = "Hello";
    ...
    c[10] = 'z'; // out of bounds, or in this case, writing into read-only memory
    

    Or maybe this:

    char *c = new char[10];
    ...
    delete [] c;
    ...
    c[2] = 'z'; // accessing freed memory
    

    Same basic principle in each case - you're doing something with memory that isn't yours.

提交回复
热议问题