Jump to content

  • Log In with Google      Sign In   
  • Create Account


NULL vs 0


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
45 replies to this topic

Poll: NULL vs 0 (81 member(s) have cast votes)

When nullptr isn't available, which do you prefer? Why?

  1. NULL (48 votes [59.26%] - View)

    Percentage of vote: 59.26%

  2. 0 (29 votes [35.80%] - View)

    Percentage of vote: 35.80%

  3. Custom type (4 votes [4.94%] - View)

    Percentage of vote: 4.94%

Vote Guests cannot vote

#41 wqking   Members   -  Reputation: 756

Like
0Likes
Like

Posted 15 December 2012 - 10:50 AM

NULL is #defined as 0, 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.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


Sponsor:

#42 Bubsy   Members   -  Reputation: 407

Like
0Likes
Like

Posted 15 December 2012 - 10:55 AM

IMO using NULL makes the intent clearer.

#43 Cornstalks   Crossbones+   -  Reputation: 6966

Like
3Likes
Like

Posted 15 December 2012 - 12:02 PM


NULL is #defined as 0, 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.

Not in a standard complying C++ implementation. The C++ standard requires NULL to evaluate to zero (that is, NULL == 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-defined 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 std::nullptr_t

Since NULL 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 NULL evaluates to zero.
[ 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 ]

#44 SiCrane   Moderators   -  Reputation: 9418

Like
0Likes
Like

Posted 15 December 2012 - 01:13 PM

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.

#45 iMalc   Crossbones+   -  Reputation: 2260

Like
2Likes
Like

Posted 15 December 2012 - 04:17 PM

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.

Edited by iMalc, 15 December 2012 - 04:18 PM.

"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

#46 wqking   Members   -  Reputation: 756

Like
0Likes
Like

Posted 15 December 2012 - 08:40 PM

Not in a standard complying C++ implementation. The C++ standard requires NULL to evaluate to zero (that is, NULL == 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-defined 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 std::nullptr_t

Since NULL 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 NULL evaluates to zero.

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

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS