如果取消引用空指针,为什么操作系统不会崩溃? [英] Why doesn't the OS crash if I dereference a null pointer?

查看:62
本文介绍了如果取消引用空指针,为什么操作系统不会崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

取消引用空指针会导致未定义的行为.实际上,通常意味着我的程序将崩溃.但是,为什么操作系统不会崩溃?因为如果我的程序取消引用空指针,并且我的程序由OS运行,则根据逻辑可传递性规则,这意味着OS试图取消引用空指针.为什么操作系统不进入未定义行为"状态?

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 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"?

推荐答案

C ++标准没有定义行为,以保证崩溃或进行其他任何操作.这不会阻止OS定义行为-它不是C ++程序,因此它不必遵守C ++程序的规则" [1] .即使这样,操作系统也不会取消对指针本身的引用.

The C++ standard doesn't define the behaviour, either to guarantee a crash, or to do anything else. That doesn't prevent the OS from defining the behaviour - it's not a C++ program, so it doesn't have to abide by the "rules"[1] of C++ programs. Even so, the OS won't dereference the pointer itself.

在大多数现代平台上,访问已取消引用指针的目标将导致内存管理硬件引发异常(通常称为分段故障"或保护故障").这由内核捕获,内核可以确定执行该进程的进程,然后终止该进程或向其发送信号.

On most modern platforms, accessing the target of the dereferenced pointer will cause the memory-management hardware to raise an exception (often called a "segmentation fault" or "protection fault"). This is caught by the kernel, which can determine which process did it, and either kill the process, or send it a signal.

因此,在这样的平台上,取消引用空指针的进程的默认行为将崩溃.操作系统本身没有任何崩溃的原因.

So, on such a platform, the default behaviour of a process that dereferences a null pointer will be to crash; there is no reason whatsoever for the OS itself to crash.

[1] 我的意思是"非正式的规则",即程序应该格式正确并避免未定义的行为-不要与C ++的正式的规则"相混淆语言标准指定的实现.

这篇关于如果取消引用空指针,为什么操作系统不会崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆