C++ 标准:取消引用 NULL 指针以获取引用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2727834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ standard: dereferencing NULL pointer to get a reference?
提问by shoosh
I'm wondering about what the C++ standard says about code like this:
我想知道 C++ 标准对这样的代码是怎么说的:
int* ptr = NULL;
int& ref = *ptr;
int* ptr2 = &ref;
In practice the result is that ptr2
is NULL but I'm wondering, is this just an implementation detail or is this well defined in the standard?
Under different circumstances a dereferencing of a NULL pointer should result in a crash but here I'm dereferencing it to get a reference which is implemented by the compiler as a pointer so there's really no actual dereferencing of NULL.
在实践中,结果是ptr2
NULL 但我想知道,这只是一个实现细节还是在标准中定义得很好?
在不同的情况下,取消引用 NULL 指针应该会导致崩溃,但在这里我取消引用它以获取由编译器作为指针实现的引用,因此实际上没有实际取消引用 NULL。
回答by Michael Burr
Dereferencing a NULL pointer is undefined behavior.
取消引用 NULL 指针是未定义的行为。
In fact the standard calls this exact situation out in a note (8.3.2/4 "References"):
事实上,标准在注释(8.3.2/4“参考”)中指出了这种确切的情况:
Note: in particular, a null reference cannot exist in a well-defined program, because the only way to create such a reference would be to bind it to the “object” obtained by dereferencing a null pointer, which causes undefined behavior.
注意:特别是,在定义良好的程序中不能存在空引用,因为创建这种引用的唯一方法是将其绑定到通过取消引用空指针获得的“对象”,这会导致未定义的行为。
As an aside: The one time I'm aware of that a NULL pointer can be "dereferenced" in a well-defined way is as the operand to the sizeof
operator, because the operand to sizeof
isn't actually evaluated (so the dereference never actually occurs).
顺便说一句:有一次我意识到可以以明确定义的方式“取消引用” NULL 指针是作为运算sizeof
符的操作数,因为sizeof
实际上并未评估操作数(因此取消引用实际上从未发生)。
回答by Gorpik
Dereferencing a NULL pointer is explicitly undefined behaviour in the C++ standard, so what you see is implementation specific.
取消引用 NULL 指针是 C++ 标准中明确未定义的行为,因此您看到的是特定于实现的。
Copying from 1.9.4 in the C++0x draft standard (similar to previous standards in this respect):
从 C++0x 草案标准中的 1.9.4 复制(在这方面类似于以前的标准):
Certain other operations are described in this International Standard as undefined (for example, the effect of dereferencing the null pointer). [Note: this International Standard imposes no requirements on the behavior of programs that contain undefined behavior. - end note]
本国际标准中将某些其他操作描述为未定义(例如,取消引用空指针的效果)。[注意:本国际标准对包含未定义行为的程序的行为没有要求。-尾注]
回答by shoosh
For completeness, this: http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#232talks specifically about this issue.
为了完整起见,这个:http: //www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#232专门讨论了这个问题。
回答by Goz
Dereferencing a NULL pointer is undefined behaviour. You should check if a value is NULL before dereferencing it.
取消引用 NULL 指针是未定义的行为。您应该在取消引用之前检查值是否为 NULL。
回答by valdo
int& ref = *ptr;
The above statement doesn't actually dereference anything. So there's no problem until you use the ref
(which is invalid).
上面的语句实际上并没有取消引用任何东西。因此,在您使用ref
(无效)之前没有问题。