问题描述
我想知道在C ++中是否保证 NULL
为 0
,所以我进行了搜索并发现了以下内容:
I was wondering if NULL
is guaranteed to be 0
in C++, so I searched and came across these:
此答案指出:
似乎可以确认 NULL
始终为0.
Which seems to confirm that NULL
is always 0.
但是根据 cppreference.com :
#define NULL /*implementation-defined*/
宏NULL是实现定义的空指针常量,可能是:
The macro NULL is an implementation-defined null pointer constant, which may be:
->整数类型的整数常量表达式右值,其值是到零(直到C ++ 11)
-> an integral constant expression rvalue of integer type that evaluates to zero (until C++11)
->整数文字,其值为零或prvalue类型std :: nullptr_t(自C ++ 11起)
-> an integer literal with value zero, or a prvalue of type std::nullptr_t (since C++11)
这显然表明 NULL
是依赖于实现的.
And that clearly says NULL
is implementation dependent.
此答案是:
f(int);
f(foo *);
再次暗示 NULL
是整数 0
,这可能会引起歧义
Which again implies that NULL
is the integer 0
and that might cause ambiguity
我遇到了其他问题和答案,但它们大多与C语言有关,而不与C ++有关.此评论说:>
There are other questions and answers I encountered, but they are mostly about the C language, not C++. This comment says:
但同样,那是关于C的.
But again, that's about C.
总而言之,在C ++中, NULL
是否总是 0
?C呢?(对于每个标准实现)是否都与
To sum it all up, is NULL
always 0
in C++? What about C? Is it (for every standard implementation) the same as:
#define NULL 0
注意:这个问题与空指针无关,问题在于是否保证C ++中的 NULL
是整数 0
.是否依赖于实现?
Note: this question is not about the null pointer, the question is if NULL
in C++ is guaranteed to be 0
the integer. Is it implementation dependent?
推荐答案
根据标准, NULL
是一个空指针常量(即文字).究竟是哪个定义了实现.
According to the standard, NULL
is a null pointer constant (i.e. literal). Exactly which one, is implementation defined.
在C ++ 11之前,空指针常量是整数值,其整数值等于0,因此 0
或 0l
等.
Prior to C++11, null pointer constants were integral constants whose integral value is equal to 0, so 0
or 0l
etc.
自C ++ 11起,有一个新的空指针文字 nullptr
,并且 NULL
可以定义为 nullptr
.(因此,对Bjarne的报价的字面解释已经过时了.)
Since C++11, there is a new null pointer literal nullptr
and NULL
may be defined as being nullptr
. (And thus literal interpretation of Bjarne's quote has become obsolete).
在标准化之前: NULL
在C中可以定义为(void *)0
.由于C ++基于C,所以某些C ++方言可能会约会标准可能使用了该定义,但此定义不符合标准C ++.
Prior to standardisation: NULL
may be defined as (void*)0
in C. Since C++ was based on C, it is likely that some C++ dialects pre-dating the standard might have used that definition, but such definition is not conformant with standard C++.
为了完整起见:正如下面的注释中链接的SO post中所详细说明的那样,空指针常量为0并不一定意味着空指针地址的值为0(尽管地址为0是很典型的)
And for completeness: As explained in more detail in SO post linked in a comment below, null pointer constant being 0 does not necessarily mean that the value of the null pointer address is 0 (although the address being 0 is quite typical).
对此可以得出什么结论
- 不要使用
NULL
表示数字零(如果合适,请使用带有适当类型后缀的0
),也不要表示一个以空字符结尾的字符(使用>'\ 0'
). - 不要假定
NULL
会解决指针重载. - 要表示空指针,请不要使用
NULL
,而如果您的标准是> = C ++ 11,请使用nullptr
.在较旧的标准中,如果您需要使用它来解决过载,则可以使用(T *)NULL
或(T *)0
.用指针重载整数毫无意义. - 考虑到从C转换为C ++时的定义可能不同,反之亦然.
- 不要将零位存储(或键入pun)零位到指针中.不能保证它是空指针.
- Don't use
NULL
to represent the number zero (use0
with appropriate type suffix if appropriate), nor to represent a null-terminator character (use'\0'
). - Don't assume that
NULL
resolves to a pointer overload. - To represent a null pointer, don't use
NULL
but instead usenullptr
if your standard is >= C++11. In older standard you can use(T*)NULL
or(T*)0
if you need it for overload resolution... that said there are probably very few cases where overloading integers with pointers makes any sense. - Consider that the definition may differ when converting from C to C++ and vice versa.
- Don't memset (or type pun) zero bits into a pointer. That's not guaranteed to be the null pointer.
这篇关于NULL是否保证为0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!