NULL是否保证为0? [英] Is NULL guaranteed to be 0?

查看:67
本文介绍了NULL是否保证为0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在C ++中是否保证 NULL 0 ,所以我进行了搜索并发现了以下内容:

I was wondering if NULL is guaranteed to be 0 in C++, so I searched and came across these:

答案指出:

这是Bjarne Stroustrup的措辞,

Here is Bjarne Stroustrup's wordings,

在C ++中,NULL的定义为0,因此只有一种美感.区别.我更喜欢避免使用宏,因此我使用0.另一个问题NULL是指人们有时会错误地认为它与众不同从0开始,并且/或者不是整数.

In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer.

似乎可以确认 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.

答案是:

0(又称C的NULL桥接到C ++)可能会导致重载的函数解析,其中包括:

0 (aka. C's NULL bridged over into C++) could cause ambiguity in overloaded function resolution, among other things:

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:

并且保证NULL为0

And NULL is guaranteed to be 0

但同样,那是关于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为0?

Is NULL guaranteed to be 0?

根据标准, 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 (use 0 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 use nullptr 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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