在c++的早期,当它被固定在C的顶部时,你不能使用NULL,因为它被定义为(void*)0。你不能将NULL赋值给除void*以外的任何指针,这使得它有点无用。在那些日子里,人们接受使用0(零)作为空指针。

直到今天,我仍然使用0作为空指针,但我周围的人坚持使用null。我个人认为给一个现有的值命名(NULL)没有任何好处-因为我也喜欢将指针作为真值测试:

if (p && !q)
  do_something();

那么使用0就更有意义了(就像如果你使用NULL,你不能在逻辑上使用p && !q -你需要显式地与NULL进行比较,除非你假设NULL为零,在这种情况下为什么要使用NULL)。

是否有任何客观的原因让人们更喜欢0而不是NULL(反之亦然),或者这只是个人偏好?

编辑:我应该补充(最初的意思是说),对于RAII和异常,我很少使用零/NULL指针,但有时你仍然需要它们。


当前回答

如果我回忆正确,NULL在我使用的头中定义不同。对于C,它被定义为(void*)0,而对于c++,它被定义为0。代码看起来像这样:

#ifndef __cplusplus
#define NULL (void*)0
#else
#define NULL 0
#endif

就我个人而言,我仍然使用NULL值来表示空指针,这表明你使用的是指针而不是某种整型。是的,在内部NULL值仍然是0,但它不是这样表示的。

此外,我不依赖于整数到布尔值的自动转换,而是显式地比较它们。

例如,更喜欢使用:

if (pointer_value != NULL || integer_value == 0)

而不是:

if (pointer_value || !integer_value)

可以这么说,这在c++ 11中都得到了纠正,可以简单地使用nullptr而不是NULL,也可以使用nullptr的类型nullptr_t。

其他回答

我主张在任何可能的情况下都不要使用0或NULL指针。

使用它们迟早会导致你的代码出现分段错误。根据我的经验,指针是c++中最大的bug来源之一

此外,它还会导致在整个代码中出现“if-not-null”语句。如果您可以依赖始终有效的状态,那就更好了。

几乎总是有更好的选择。

奇怪,没有人,包括Stroustroup提到过。虽然谈论了很多标准和美学,但没有人注意到用0代替NULL是危险的,例如,在架构的变量参数列表中sizeof(int) != sizeof(void*)。像Stroustroup一样,出于审美原因,我更喜欢0,但必须小心不要在其类型可能不明确的地方使用它。

我认为有一些论点(其中一个是最近的)与Bjarne在这方面的立场相矛盾。

Documentation of intent Using NULL allows for searches on its use and it also highlights that the developer wanted to use a NULL pointer, irrespective of whether it is being interpreted by the compiler as NULL or not. Overload of pointer and 'int' is relatively rare The example that everybody quotes is: void foo(int*); void foo (int); void bar() { foo (NULL); // Calls 'foo(int)' } However, at least in my opinion, the problem with the above is not that we're using NULL for the null pointer constant: it's that we have overloads of foo() which take very different kinds of arguments. The parameter must be an int too, as any other type will result in an ambiguous call and so generate a helpful compiler warning. Analysis tools can help TODAY! Even in the absence of C++0x, there are tools available today that verify that NULL is being used for pointers, and that 0 is being used for integral types. C++ 11 will have a new std::nullptr_t type. This is the newest argument to the table. The problem of 0 and NULL is being actively addressed for C++0x, and you can guarantee that for every implementation that provides NULL, the very first thing that they will do is: #define NULL nullptr For those who use NULL rather than 0, the change will be an improvement in type-safety with little or no effort - if anything it may also catch a few bugs where they've used NULL for 0. For anybody using 0 today... well, hopefully they have a good knowledge of regular expressions...

我总是用0。不是因为什么真正的原因,只是因为当我第一次学习c++时,我读到一些建议使用0的东西,我总是这样做。理论上,在可读性方面可能会出现混淆问题,但实际上,在数千个工时和数百万行代码中,我从未遇到过这样的问题。正如Stroustrup所说,在标准变成nullptr之前,这实际上只是一个个人审美问题。

主要是个人偏好,尽管有人可能会说,NULL使对象很明显是一个指针,目前不指向任何东西,例如。

void *ptr = &something;
/* lots o' code */
ptr = NULL; // more obvious that it's a pointer and not being used

IIRC,标准不要求NULL为0,所以使用<stddef.h>中定义的任何东西可能对你的编译器是最好的。

争论的另一个方面是是否应该使用逻辑比较(隐式转换为bool)或对NULL进行显式检查,但这也归结于可读性。