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


当前回答

将指针设置为0并不那么清楚。特别是如果你使用c++以外的语言。这包括C语言和Javascript。

我最近用这样的代码删除:

DrawTo(BITMAP *buffer) =0;

第一次为纯虚函数。我以为这只是一个星期的魔术表演。当我意识到它基本上只是将函数指针设置为空(因为在c++的大多数情况下虚函数只是函数指针)时,我踢了自己一脚。

DrawTo(BITMAP *buffer) =null;

如果我的新眼睛没有间隔的话就不会那么令人困惑了。实际上,我想知道为什么c++不使用小写的null,就像它现在使用小写的false和true一样。

其他回答

    cerr << sizeof(0) << endl;
    cerr << sizeof(NULL) << endl;
    cerr << sizeof(void*) << endl;

    ============
    On a 64-bit gcc RHEL platform you get:
    4
    8
    8
    ================

这个故事的寓意。在处理指针时应该使用NULL。

1)它声明了你的意图(不要让我搜索你所有的代码,试图找出一个变量是指针还是一些数字类型)。

2)在某些需要变量参数的API调用中,它们将使用null指针来指示参数列表的结束。在这种情况下,使用“0”而不是NULL可能会导致问题。在64位平台上,va_arg调用需要一个64位指针,但您将只传递一个32位整数。在我看来,你是在依靠其他32位来为你归零?我见过某些编译器(例如英特尔的icpc)不是那么友好——这导致了运行时错误。

我认为标准保证NULL == 0,所以你可以做任何一件事。我更喜欢NULL,因为它记录了您的意图。

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

下面是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.

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

我认为有一些论点(其中一个是最近的)与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...