NULL vs 0

Started by
43 comments, last by wqking 11 years, 4 months ago

[font=courier new,courier,monospace]NULL[/font] is [font=courier new,courier,monospace]#define[/font]d as [font=courier new,courier,monospace]0[/font], meaning there's absolutely no difference except style/personal preference, and Bjarne "C++" Stroustrup was quoted earlier as preferring 0 (or nullptr in C++11 where available) over the NULL macro.

As far as I understand, C++ does not require NULL to be zero.
It can be any integer beside 0.
I can not remember where I read, but on some embed system, NULL can be a value different than zero.
So he is correct.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

Advertisement
IMO using NULL makes the intent clearer.

[quote name='Hodgman' timestamp='1355570648' post='5010908']
[font=courier new,courier,monospace]NULL[/font] is [font=courier new,courier,monospace]#define[/font]d as [font=courier new,courier,monospace]0[/font], meaning there's absolutely no difference except style/personal preference, and Bjarne "C++" Stroustrup was quoted earlier as preferring 0 (or nullptr in C++11 where available) over the NULL macro.

As far as I understand, C++ does not require NULL to be zero.
It can be any integer beside 0.
I can not remember where I read, but on some embed system, NULL can be a value different than zero.
So he is correct.
[/quote]
Not in a standard complying C++ implementation. The C++ standard requires [font=courier new,courier,monospace]NULL[/font] to evaluate to zero (that is, [font=courier new,courier,monospace]NULL[/font] == 0 is always true). Embedded systems implementations sometimes break the rules of C++, so they're not exactly the best reference. If you don't believe me, read the C++ Standard:

Section 18.2, paragraph 3: The macro NULL is an implementation-de?ned C++ null pointer constant in this International Standard
Section 4.10, paragraph 1: A null pointer constant is an integral constant expression prvalue of integer type that evaluates to zero or a prvalue of type [font=courier new,courier,monospace]std::nullptr_t[/font]

Since [font=courier new,courier,monospace]NULL[/font] is required to be a "null pointer constant" and a "null pointer constant is an integral constant expression [...] that evaluates to zero" we can deduct that [font=courier new,courier,monospace]NULL[/font] evaluates to zero.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

I can not remember where I read, but on some embed system, NULL can be a value different than zero.

What you might be thinking of is the actual representation on the machine being different from zero, which is allowed by the standard. It's also legal for some pointers to have a zero binary representation, but others have a non-zero binary representation. For instance, with MSVC most pointers use 0 for the actual representation of a null pointer, but pointers to members have the value 0xFFFFFFFF on 32-bit builds. However, no matter what value the machine actually sees, you can't use a non-zero integral constant to represent the null pointer.
Macros are a fact of C & C++ programming. e.g. You don't get compiler independent include guards, or a whole heap of other useful stuff like half of boost, without them. Claiming all macros are evil is like any other claim that all XYZ are bad. There are always degrees of bad, and there are always exceptions.

NULL is like the least evil macro there is.
Use the tools you are provided with, in the way that they were intended to be used, that shows the most clear intentions.
The real problems are caused when you fight against the language / tools, and write unclear code.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

Not in a standard complying C++ implementation. The C++ standard requires [font=courier new,courier,monospace]NULL[/font] to evaluate to zero (that is, [font=courier new,courier,monospace]NULL[/font] == 0 is always true). Embedded systems implementations sometimes break the rules of C++, so they're not exactly the best reference. If you don't believe me, read the C++ Standard:

Section 18.2, paragraph 3: The macro NULL is an implementation-de?ned C++ null pointer constant in this International Standard
Section 4.10, paragraph 1: A null pointer constant is an integral constant expression prvalue of integer type that evaluates to zero or a prvalue of type [font=courier new,courier,monospace]std::nullptr_t[/font]

Since [font=courier new,courier,monospace]NULL[/font] is required to be a "null pointer constant" and a "null pointer constant is an integral constant expression [...] that evaluates to zero" we can deduct that [font=courier new,courier,monospace]NULL[/font] evaluates to zero.

Thanks for pointing it out.
Knowing standard better is always good. :)

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

This topic is closed to new replies.

Advertisement