Why doesn't the OS crash if I dereference a null pointer?

后端 未结 9 1421
慢半拍i
慢半拍i 2021-01-21 01:21

Dereferencing a null pointer results in undefined behavior. In practice it usually means that my program will crash. But why doesn\'t the OS crash? Because if my progra

9条回答
  •  走了就别回头了
    2021-01-21 01:58

    Memory access is protected in every major OS. You cannot simply write a program that manipulates memory that was not allocated for it (assuming a pointer is not initialized for example, it could be ANY address). So, every time a program tries to access some address space that does not belong to it, the OS will send a signal to terminate the program (resulting in the ultimate famous "Segmentation fault", familiar to any C/C++ programmer).

提交回复
热议问题