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

后端 未结 9 1416
慢半拍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:19

    Because if my program dereferences a null pointer, and my program is run by the OS, then, according to the rules of logical transitivity, this means the OS tried to dereference a null pointer. Why doesn't the OS enter a state of "undefined behavior"?

    This is wrong. There's something called memory protection and thats WHY your program is terminated. Is the OS that is protecting itself (in the terms of memory use).

提交回复
热议问题