C++ 我应该写“0x0”还是“0”而不是NULL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1765171/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:07:33  来源:igfitidea点击:

Instead of NULL, should I write `0x0` or `0`?

c++null

提问by moka

I know that you are supposed to use 0instead of NULL in c++ (even though NULLis defined as 0in C++ most of the time).

我知道你应该0在 c++ 中使用而不是 NULL (即使大多数时候在 C++ 中NULL定义为0)。

Recently I came across some code where 0x0was used instead, though.

不过,最近我遇到了一些0x0被使用的代码。

What is the difference?

有什么不同?

回答by Mehrdad Afshari

0x0is just 0written in hexadecimal notation. There is no difference between the two:

0x0只是0用十六进制表示法写的。两者没有区别:

016= 010:)

0 16= 0 10:)

NULLis usually #defined to 0somewhere and does the same thing.

NULL通常是#defined 到0某个地方并做同样的事情。

回答by Jerry Coffin

There is no difference. In C, NULL is often defined to be (void *)0, but in C++ that's not allowed. A few ancient compilers got this wrong, but they really are ancient.

没有区别。在 C 中,NULL 通常被定义为(void *)0,但在 C++ 中这是不允许的。一些古老的编译器弄错了,但它们确实很古老。

IMO, it's better to use NULL, as it portrays the intentmore clearly, and gives you a nice, easy symbol to S&R when your compiler gets updated to C++ 0x, which will include nullptr.

IMO,最好使用 NULL,因为它更清楚地描绘了意图,并在您的编译器更新为 C++ 0x 时为您提供了一个漂亮、简单的 S&R 符号,其中将包括nullptr.

回答by tony

By the way, everyone,

顺便说一下,大家,

0x0 is hex, as you have all mentioned, but

0x0 是十六进制,正如你所提到的,但是

0 is Octal, not decimal! :-)

0 是八进制,不是十进制!:-)

ie any number starting with 0 (and not followed by x) is octal:

即任何以 0 开头(后面没有 x)的数字都是八进制:

0 = 0
01 = 1
02 = 2
...
07 = 7
010 = 8
011 = 9
012 = 10
...

:-)

:-)

回答by Michael Kristofik

0x0and 0represent the same value, so you can use the one that best suits your eye. When we get C++0x, we'll have the keyword nullptrto represent null pointers of any type.

0x00代表相同的值,因此您可以使用最适合您眼睛的值。当我们得到 C++0x 时,我们将有关键字nullptr来表示任何类型的空指针。

回答by postfuturist

0x0 is just an expression of the value 0 in hexadecimal. I doubt the compiler will care about the difference.

0x0 只是值 0 的十六进制表达式。我怀疑编译器会关心差异。

回答by Tom

No difference

没有不同

In my opinion, 0x0 is more explicit.

在我看来,0x0 更明确。

Some people may confuse 0 with "0"(0x30 -I′ve seen it happen).

有些人可能会将 0 与“0”混淆(0x30 - 我见过它发生过)。

回答by Shailesh Kumar

Simply put, its a matter of taste. People like to write memory addresses in hexa decimal, so writing NULL as 0x0 would be more classic. For ordinary ones like me, 0 will suffice.

简单地说,它是一个品味问题。人们喜欢用十六进制写内存地址,所以写 NULL 为 0x0 会更经典。对于我这种普通人,0就够了。

回答by poundifdef

So, this site has some really good information on NULL: http://c-faq.com/null/index.html

所以,这个站点有一些关于 NULL 的非常好的信息:http: //c-faq.com/null/index.html

NULL is actually the "null pointer" - so it might be of type, for example, "(void*)0". This tells the compiler that NULL is not to be treated as an int, but rather as a "pointer to an int". And in C, the compiler will give warnings/errors if the types don't match.

NULL 实际上是“空指针”——所以它可能是类型,例如,“(void*)0”。这告诉编译器不要将 NULL 视为 int,而是将其视为“指向 int 的指针”。在 C 中,如果类型不匹配,编译器将给出警告/错误。

The value "0" and "0x0" are equivalent in C++. Whenever a number is prefixed by "0x", it means that that value is the hexadecimal representation of that number. For example, 0x5 == 5.

值“0”和“0x0”在 C++ 中是等价的。每当一个数字以“0x”为前缀时,就意味着该值是该数字的十六进制表示。例如,0x5 == 5。

Also, 0xA [in hexadecimal, base 16) == 10 [in base 10].

此外,0xA [十六进制,基数 16) == 10 [基数 10]。

edit

编辑

Here are some sources. In glibc, NULL seems to be defined differently in different places. (why should this be?)

这里有一些来源。在 glibc 中,NULL 似乎在不同地方的定义不同。(为什么会这样?)

#define NULL ((void *)0)(stdlib/gmp-impl.h)

#define NULL ((void *)0)(stdlib/gmp-impl.h)

#define NULL 0(posix/getopt1.c)

#define NULL 0(posix/getopt1.c)

edit2

编辑2

well, it looks like i've missed the mark on this one! sorry! (CW'd. feel free to keep downvoting!)

好吧,看来我错过了这一点!对不起!(CW'd。随时保持downvote!)