为什么解引用一个空指针是未定义的行为? [英] Why dereferencing a null pointer is undefined behaviour?

查看:766
本文介绍了为什么解引用一个空指针是未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据ISO C ++,取消引用空指针是未定义的行为。我的好奇心是,为什么?为什么标准已经决定声明它未定义的行为?这个决定背后的理由是什么?编译器依赖?似乎没有,因为根据C99标准,据我所知,它是很明确的。机器依赖?任何想法?

According to ISO C++, dereferencing a null pointer is undefined behaviour. My curiosity is, why? Why standard has decided to declare it undefined behaviour? What is the rationale behind this decision? Compiler dependency? Doesn't seem, because according to C99 standard, as far as I know, it is well defined. Machine dependency? Any ideas?

推荐答案

定义一致行为解除引用NULL指针需要编译器在每个解引用之前检查NULL指针CPU架构。这对于一个为速度而设计的语言来说是不可接受的。

Defining consistent behavior for dereferencing a NULL pointer would require the compiler to check for NULL pointers before each dereference on most CPU architectures. This is an unacceptable burdern for a language that is designed for speed.

它只修复了一个更大问题的一小部分 - 有许多方法有无效的指针超出NULL指针。

It also only fixes a small part of a larger problem - there are many ways to have an invalid pointer beyond a NULL pointer.

这篇关于为什么解引用一个空指针是未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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