NULL与0 [英] NULL vs. 0

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

问题描述

我搜索了这个问题的常见问题解答,无法真正找到答案。


从风格上讲,使用0或NULL更好吗?

我知道预标准,0是推荐的,因为有些编译器

不正确地实现了NULL。


但是,18.1 / 4(脚注) 180)表示0和0L是有效的NULL,

而(void *)0明显无效。


因此,假设(合理)标准符合编译器,是否有一个

的理由继续优先于0而不是NULL? NULL似乎具有

优势,表明我们正在检查一个指针。


另外,nullptr使它进入C ++ 0x草案?

I searched the FAQ on this one, couldn''t really find an answer.

Stylistically, is it better to use 0 or NULL?

I know that pre-Standard, 0 was recommended, because some compilers
implemented NULL improperly.

However, 18.1/4(footnote 180) indicates that 0 and 0L are valid NULLs,
while (void*)0 is explicitly invalid.

So, assuming a (reasonably) Standard compliant compiler, is there a
reason to continue to prefer 0 over NULL? NULL would seem to have the
advantage of indicating that we''re checking a pointer.

Also, did nullptr make it into the C++0x draft?

推荐答案

>来自The C ++ Language:


Zero(0)is一个int。由于标准转换,0可以用作任何积分,浮点,指针或指针成员

类型的

常量。零的类型将由上下文确定。零将

通常(但不总是)由全零的位模式表示

的适当大小。

无对象分配地址为0.因此,0充当

指针文字,表示指针没有引用

对象。

在C中,定义一个宏NULL代表一个零

指针很受欢迎。由于C ++的类型检查更严格,使用普通0,

而不是任何建议的NULL宏,可以减少问题。

red floyd写道:
>From "The C++ Language":

Zero (0) is an int. Because of standard conversions, 0 can be used as a
constant of any integral, floating-point, pointer, or pointer-to-member
type. The type of zero will be determined by context. Zero will
typically (but not always) be represented by a bit-pattern of all-zeros
of the appropriate size.
No object is allocated with the address of 0. Consequently, 0 acts as a
pointer literal, indicating that the pointer doesn''t refer to an
object.
In C, it has been popular to define a macro NULL to represent a zero
pointer. Because of C++''s tighter type checking, the use of plain 0,
rather than any suggested NULL macro, leads to fewer problems.
red floyd wrote:
我搜索了这个问题的常见问题解答,无法找到答案。

从风格上看,使用0还是NULL更好?

我知道pre-Standard,0是推荐的,因为有些编译器不正确地实现了NULL。

然而,18.1 / 4(脚注180)表明0和0L是有效的NULL,
while(void *)0显然无效。

因此,假设一个(合理的)符合标准的编译器,是否有理由继续优先于0而不是NULL? NULL似乎具有指示我们正在检查指针的优点。

另外,nullptr是否使它进入C ++ 0x草案?
I searched the FAQ on this one, couldn''t really find an answer.

Stylistically, is it better to use 0 or NULL?

I know that pre-Standard, 0 was recommended, because some compilers
implemented NULL improperly.

However, 18.1/4(footnote 180) indicates that 0 and 0L are valid NULLs,
while (void*)0 is explicitly invalid.

So, assuming a (reasonably) Standard compliant compiler, is there a
reason to continue to prefer 0 over NULL? NULL would seem to have the
advantage of indicating that we''re checking a pointer.

Also, did nullptr make it into the C++0x draft?






ra****@gmail.com 写道:
ra****@gmail.com wrote:
red floyd写道:
red floyd wrote:
我搜索了这个问题的常见问题解答,无法找到答案。

风格,使用0或NULL更好吗?

我知道预标准,0推荐,因为有些编译器实现了NULL不正确。

但是, 18.1 / 4(脚注180)表示0和0L是有效的NULL,
而(void *)0明显无效。

因此,假设(合理的)符合标准的编译器,有理由继续优先于0而不是NULL吗? NULL似乎具有指示我们正在检查指针的优点。

另外,nullptr是否使它进入C ++ 0x草案?
I searched the FAQ on this one, couldn''t really find an answer.

Stylistically, is it better to use 0 or NULL?

I know that pre-Standard, 0 was recommended, because some compilers
implemented NULL improperly.

However, 18.1/4(footnote 180) indicates that 0 and 0L are valid NULLs,
while (void*)0 is explicitly invalid.

So, assuming a (reasonably) Standard compliant compiler, is there a
reason to continue to prefer 0 over NULL? NULL would seem to have the
advantage of indicating that we''re checking a pointer.

Also, did nullptr make it into the C++0x draft?


FromThe C ++ Language:
From "The C++ Language":



零(0)是一个int。由于标准转换,0可用作任何整数,浮点,指针或指向成员的类型的常量。零的类型将由上下文确定。零通常(但不总是)由适当大小的全零的位模式表示。
没有分配地址为0的对象。因此,0表示作为
指针文字,表示指针没有引用
对象。
在C中,定义宏NULL表示零已经很流行了
指针。由于C ++的类型检查更严格,使用普通0,而不是任何建议的NULL宏,可以减少问题。



Zero (0) is an int. Because of standard conversions, 0 can be used as a
constant of any integral, floating-point, pointer, or pointer-to-member
type. The type of zero will be determined by context. Zero will
typically (but not always) be represented by a bit-pattern of all-zeros
of the appropriate size.
No object is allocated with the address of 0. Consequently, 0 acts as a
pointer literal, indicating that the pointer doesn''t refer to an
object.
In C, it has been popular to define a macro NULL to represent a zero
pointer. Because of C++''s tighter type checking, the use of plain 0,
rather than any suggested NULL macro, leads to fewer problems.



1.请不要'' t top post

http://www.parashift.com/c++-faq-lit....html#faq-5.4)

重新排列以符合cl c ++网络礼仪。


2.我有,并且已经阅读了TC ++ PL3e。我的观点是因为

标准18.1 / 4在本国际标准(4.10)中指定宏NULL是实现定义的

空指针常量。脚注

180到18.1 / 4特别禁止(void *)0,是否有任何好的现代

理由再使用0而不是NULL?


1. Please don''t top post
(http://www.parashift.com/c++-faq-lit....html#faq-5.4).
Rearranged to conform to c.l.c++ netiquette.

2. I have, and have read TC++PL3e. My point was that because the
Standard 18.1/4 specifies "The macro NULL is an implementation-defined
null pointer constant in this International Standard (4.10)." Footnote
180 to 18.1/4 specifically disallows (void*)0, is there any good modern
reason to use 0 instead of NULL anymore?


red floyd写道:
red floyd wrote:
...
因此,假设一个(合理的)符合标准的编译器,是否有理由继续偏好0而不是NULL? NULL似乎具有指示我们正在检查指针的优点。
...
...
So, assuming a (reasonably) Standard compliant compiler, is there a
reason to continue to prefer 0 over NULL? NULL would seem to have the
advantage of indicating that we''re checking a pointer.
...




因为这个原因它在指针上下文中更喜欢NULL超过0。

否则,它们是等价的(在这种情况下)。


-

祝你好运,

Andrey Tarasevich



For that very reason it makes sense to prefer NULL over 0 in pointer context.
Otherwise, they are equivalent (in such contexts).

--
Best regards,
Andrey Tarasevich


这篇关于NULL与0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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