问题描述
这是博客作者带来了关于空指针dereferecing讨论:
An blog author has brought up the discussion about null pointer dereferecing:
- http://bit.ly/1L98GL4
他的理由是引用标准的主线是这样的:
His main line of reasoning quoting the standard is this:
在'和; podhd-> LINE6'前pression是在C语言中未定义行为
当podhd'是一个空指针。
C99标准说,有关的下列'和;'地址运算符
(6.5.3.2地址和间接运算符):
The C99 standard says the following about the '&' address-of operator (6.5.3.2 "Address and indirection operators"):
一元和放大器的操作;运营商应是一个函数
标志,一个[]或目*运算符,或左值的结果
指定的对象不是一个位域,不与声明
寄存器存储类说明。
The operand of the unary & operator shall be either a function designator, the result of a [] or unary * operator, or an lvalue that designates an object that is not a bit-field and is not declared with the register storage-class specifier.
这位前pression'podhd-> LINE6显然不是一个功能标志,
一个[]或*运算的结果。它是一个左值前pression。然而,
当podhd指针为NULL,前pression没有指定的
因为6.3.2.3对象指针说:
The expression 'podhd->line6' is clearly not a function designator, the result of a [] or * operator. It is an lvalue expression. However, when the 'podhd' pointer is NULL, the expression does not designate an object since 6.3.2.3 "Pointers" says:
如果空指针常数转换为指针类型,
结果指针,叫做空指针,是保证比较
不等的指针的任何对象或功能。
If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
当,当它被评估一个左值没有指定一个对象,则
行为是不确定的(C99 6.3.2.1左值,数组和函数
代号):
When "an lvalue does not designate an object when it is evaluated, the behavior is undefined" (C99 6.3.2.1 "Lvalues, arrays, and function designators"):
这是左值是与对象类型或不完全类型的前pression
非void;如果当它是一个左值没有指定一个对象
评估,这种行为是未定义。
An lvalue is an expression with an object type or an incomplete type other than void; if an lvalue does not designate an object when it is evaluated, the behavior is undefined.
所以,在短暂的同样的想法:
So, the same idea in brief:
当 - >上的指针被执行死刑,其计算结果为左值在哪里
没有对象存在,因此该行为是不确定的。
When -> was executed on the pointer, it evaluated to an lvalue where no object exists, and as a result the behavior is undefined.
此问题是纯粹的语言基础,我不要求对给定的系统是否允许一个与地址0在任何语言中是什么样的篡改。
This question is purely language based, I'm not asking regarding whether a given system allows one to tamper with what lies at address 0 in any language.
据我所看到的,有一个在非关联化的指针变量,其值等于 nullptr
没有限制,甚至想到一个指针的比较对 nullptr
(或(无效*)0
)常数可以在因为规定的段落某些情况下优化消失,但是这看起来像另一个的问题,它并不prevent解引用一个指针,其值等于 nullptr
。请注意,我检查其他SO问题和答案,我特别,以及标准报价以上,而我却没有的东西,显然从标准推断跌跌撞撞,如果一个指针 PTR
比较等于 nullptr
,取消引用这将是不确定的行为。
As far as I can see, there's no restriction in dereferencing a pointer variable whose value is equal to nullptr
, even thought comparisons of a pointer against the nullptr
(or (void *) 0
) constant can vanish in optimizations in certain situations because of the stated paragraphs, but this looks like another issue, it doesn't prevent dereferencing a pointer whose value is equal to nullptr
. Notice that I've checked other SO questions and answers, I particularly like this set of quotations, as well as the standard quotes above, and I didn't stumbled upon something that clearly infers from standard that if a pointer ptr
compares equal to nullptr
, dereferencing it would be undefined behavior.
目前大多数我所得到的是deferencing的恒的(或强制转换为任何指针类型)是什么,是UB,但没有说关于一个变量,是位等于从出现的值 nullptr
。
At most what I get is that deferencing the constant (or its cast to any pointer type) is what is UB, but nothing saying about a variable that's bit equal to the value that comes up from nullptr
.
我想清楚地从持有的价值等于它的指针变量分离 nullptr
不变。但是,针对这两种情况下的答案是理想的。
I'd like to clearly separate the nullptr
constant from a pointer variable that holds a value equals to it. But an answer that address both cases is ideal.
我不明白,优化可以快速在当不会遭受 nullptr
的比较等,并可以简单地剥离基于code。
I do realise that optimizations can quick in when there're comparisons against nullptr
, etc and may simply strip code based on that.
如果得出的结论是,如果 PTR
等于 nullptr
的价值提领,绝对是UB,另一个问题如下:
If the conclusion is that, if ptr
equals to the value of nullptr
dereferencing it is definitely UB, another question follows:
推荐答案
当你引用C,提领一空指针显然从这一标准报价(重点煤矿)未定义的行为:
As you quote C, dereferencing a null pointer is clearly undefined behavior from this Standard quote (emphasis mine):
(C11,6.5.3.2p4)如果一个无效值被分配到指针,的
一元*运算符的行为是未定义 0.102),
102):在由一元*运算符取消引用指针无效的值是一个空指针,对象类型正确对齐的地址指向,以及对象的地址其生命周期结束后。
102): "Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer, an address inappropriately aligned for the type of object pointed to, and the address of an object after the end of its lifetime."
在C99相同的报价和C89 / C90相似。
Exact same quote in C99 and similar in C89 / C90.
这篇关于被取消引用指针是相等的标准nullptr未定义行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!