在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指针,但有时你仍然需要它们。


当前回答

下面是Stroustrup对此的看法:c++风格和技术常见问题解答

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. In pre-standard code, NULL was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days. If you have to name the null pointer, call it nullptr; that's what it's called in C++11. Then, nullptr will be a keyword.

也就是说,不要为小事烦恼。

其他回答

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

我更喜欢使用NULL,因为它清楚地表明,您的意图是值表示指针而不是算术值。不幸的是,它是一个宏,但由于它是如此广泛地根深蒂固,几乎没有什么危险(除非有人做了一些非常愚蠢的事情)。我希望从一开始它就是一个关键字,但是你能做什么呢?

也就是说,我对使用指针本身作为真值没有问题。就像NULL一样,这是一个根深蒂固的习惯用语。

c++ 09将添加nullptr构造,我认为这是早就该有的。

我认为有一些论点(其中一个是最近的)与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是一个有效地址,NULL被定义为一个特殊的八进制值。在该机器上(0 != NULL),因此代码如

char *p;

...

if (p) { ... }

不会如你所愿。你必须写

if (p != NULL) { ... }

虽然我相信现在大多数编译器都将NULL定义为0,但我仍然记得那些年前的教训:NULL不一定是0。

我很久以前就停止使用NULL而改用0了(以及大多数其他宏)。我这样做不仅是因为我想尽可能地避免宏,还因为NULL似乎在C和c++代码中被过度使用了。它似乎被用于任何需要0值的地方,而不仅仅是指针。

在新项目中,我把这个放在项目头中:

static const int nullptr = 0;

现在,当c++ 0x兼容的编译器到来时,我所要做的就是删除这一行。 这样做的一个好处是Visual Studio已经将nullptr识别为关键字并适当地突出显示它。