C++中NULL和0的区别

来源:互联网 发布:梦幻西游总是网络错误 编辑:程序博客网 时间:2024/06/03 18:06

今天用tinyxml2写程序,发现NULL 和 0在有些时候是相等的,以前没注意过这个问题。

所以上网搜索了一下,在stackoverflow上找到比较靠谱的回答:

http://stackoverflow.com/questions/1296843/what-is-the-difference-between-null-0-and-0

原来NULL只是个宏,下面翻译一下。

The integer constant literal 0 has different meanings depending upon the context in which it's used. In all cases, it is still an integer constant with the value 0, it is just described in different ways.

整数常量0用在不同的上下文中有不同的含义。在任何情况下,它依然是值为0的整数常量,只不过表现的方式不同。


If a pointer is being compared to the constant literal 0, then this is a check to see if the pointer is a null pointer. This 0 is then referred to as a null pointer constant. The C standard defines that 0 cast to the typevoid * is both a null pointer and a null pointer constant.

如果将一个指针和常量0作比较,这是为了检验这个指针是不是一个空指针。这个时候0相当于一个表示空指针的常量。C 标准定义转换为void *的0代表一个空指针或者空指针常量。


Additionally, to help readability, the macro NULL is provided in the header file stddef.h. Depending upon your compiler it might be possible to #undef NULL and redefine it to something wacky. Anyone doing this deserves to be shot.

并且,为了增加可读性,头文件stddef.h中提供了宏变量NULL。根据你的编译器,NULL可能没有定义或者定义成了别的东西,不过这样做事很不应该的。


Therefore, here are some valid ways to check for a null pointer:

所以,这里有一些有效的方法去检测一个空指针

if (pointer == NULL)

NULL is defined to compare equal to a null pointer. It is implementation defined what the actual definition ofNULL is, as long as it is a valid null pointer constant.

NULL用来确定一个变量是否为空指针。


if (pointer == 0)

0 is another representation of the null pointer constant.

0是空指针的另一种表达形式。

if (!pointer)

This if statement implicitly checks "is not 0", so we reverse that to mean "is 0".

这个if语句表示检测pointer是否为0.


The following are INVALID ways to check for a null pointer:

下面是检测空指针的无效方法:

int mynull = 0;<some code>if (pointer == mynull)

To the compiler this is not a check for a null pointer, but an equality check on two variables. This might work if mynull never changes in the code and the compiler optimizations constant fold the 0 into the if statement, but this is not guaranteed and the compiler has to produce at least one diagnostic message (warning or error) according to the C Standard.

对于编译器来说这不是检测一个空指针,而是检测两个变量是否相等。如果mynull在代码里面永不改变值并且编译器优化将0代入if语句中,这种方法可能有效,不过这是没有保证的。编译器可能会产生警告或者错误。


Note that what is a null pointer in the C language. It does not matter on the underlying architecture. If the underlying architecture has a null pointer value defined as address 0xDEADBEEF, then it is up to the compiler to sort this mess out.

要注意到在C语言里面空指针指的是什么。它与底层架构无关。如果底层架构定义空指针的值为地址0xDEADBEEF,那么由编译器自己决定怎么处理这种情况。


As such, even on this funny architecture, the following ways are still valid ways to check for a null pointer:

即使在那些有趣的架构里,下面依然是一些有效检测空指针的方法。

if (!pointer)if (pointer == NULL)if (pointer == 0)

The following are INVALID ways to check for a null pointer:

下面是无效的方法。

#define MYNULL (void *) 0xDEADBEEFif (pointer == MYNULL)if (pointer == 0xDEADBEEF)

as these are seen by a compiler as normal comparisons.

对于编译器来说这个只是简单的比较。

Null Characters

'\0' is defined to be a null character - that is a character with all bits set to zero. This has nothing to do with pointers. However you may see something similar to this code:

"\0"被定义为空字符。它与指针是没有关系的,但是你可能会见到与下面类似的代码:

if (!*string_pointer)

checks if the string pointer is pointing at a null character

检测指针是否指向空字符

if (*string_pointer)

checks if the string pointer is pointing at a non-null character

检测指针是否指向非空字符

Don't get these confused with null pointers. Just because the bit representation is the same, and this allows for some convenient cross over cases, they are not really the same thing.

不要把这些跟空指针混淆了。

Additionally, '\0' is (like all character literals) an integer constant, in this case with the value zero. So'\0' is completely equivalent to an unadorned 0 integer constant - the only difference is in the intent that it conveys to a human reader ("I'm using this as a null character.").

References

See Question 5.3 of the comp.lang.c FAQ for more. See this pdf for the C standard. Check out sections 6.3.2.3 Pointers, paragraph 3.



0 0
原创粉丝点击