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

后端 未结 9 1419
慢半拍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 02:15

    There are no rules of logical transitivity when it comes to UB. Your assumption is wrong.

    UB does mean that anything can happen, so on a poorly written OS, your program might actually crash the OS. Don't rule it out.

    Also, your program doesn't crash because you dereference a NULL pointer. It crashes because the OS tells it to crash.

提交回复
热议问题