为什么取消引用空指针是未定义的行为?

2023-02-13C/C++开发问题
5

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

问题描述

根据 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 指针定义一致的行为将要求编译器在大多数 CPU 架构上的每次取消引用之前检查 NULL 指针.对于专为速度而设计的语言来说,这是一种无法接受的负担.

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 burden 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.

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

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6