NULL versus 0 pointers?

Member
Posts: 321
Joined: 2004.10
Post: #1
I searched the archives and could only find stuff regarding null classes, so:

What's the latest thought on pointers initialized to NULL versus
zero?

I'm particularly interested in C++ if that makes any difference. I seem
to gotten by so far just fine using either. Looking back at old code, sometimes
I'm NULL Man and sometimes I'm Duff Man.. I mean Zero Man.

But I want to be consistant and would like to decide on one or the other.
Anybody have a definitive argument either way? I'm easily swayed.
Quote this message in a reply
Member
Posts: 111
Joined: 2002.06
Post: #2
Using NULL would make your code easier to read. Setting a variable to NULL makes it obvious that the variable is a pointer. Setting a variable to 0 would not be as clear.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #3
Yes, NULL is the standard for an uninitialized address.
Quote this message in a reply
Member
Posts: 41
Joined: 2005.02
Post: #4
WhatMeWorry Wrote:I'm particularly interested in C++ if that makes any difference. I seem
to gotten by so far just fine using either. Looking back at old code, sometimes
I'm NULL Man and sometimes I'm Duff Man.. I mean Zero Man.

In C, NULL is Zero. However, in many other languages, NULL, null, nil, or whatever is not zero, it is it's own entity. So a good habit would to always use NULL.
Quote this message in a reply
Member
Posts: 198
Joined: 2005.01
Post: #5
Also, at least in C (not sure about C++), NULL does not have to be zero. Zero can be a valid pointer value, and NULL some other invalid value.

So many people got in the habit of doing something like:

if (!foo) {

A lot of code isn't portable this way anymore, so in all practical terms there's no way anyone could make one of those systems now. I'm guessing that they standardized it in C++.

But yeah, using NULL (C++) or nil (ObjC) is definitely the recommended way.

Cryptic Allusion Games / Cryptic Allusion, LLC
http://www.cagames.com/
Quote this message in a reply
Member
Posts: 41
Joined: 2005.02
Post: #6
Dan Potter Wrote:if (!foo)

While in C, anything zero is false and anything non-zero is true - this does not happen in every other language. Getting in the habit of doing

if(foo == false)

If someone from another background read the code, it would be obvious what it meant as well.
Quote this message in a reply
Moderator
Posts: 697
Joined: 2002.04
Post: #7
I only know of one language in which "true" == 0...

Not to start another language/syntax war, but if somebody is reading C source (for whatever reason) you'd expect them to be familiar with the syntax of C – to code that way because it may be misunderstood by someone unfamiliar to the language seems like an unnecessary waste of time...

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #8
Also, things like
Code:
if (!somePointer)
    showSomeError();
are pretty clear anyway.
Quote this message in a reply
Hog
Member
Posts: 151
Joined: 2002.09
Post: #9
i always use 0 instead of NULL. i don't think NULL is a standard, i've had occurences where it wasn't even defined, and in case someone defines it as something else then things like:

Code:
if(!foo) foo->doFoo();
if(foo) delete foo;

would certainly cause trouble.
Quote this message in a reply
Hog
Member
Posts: 151
Joined: 2002.09
Post: #10
szymczyk Wrote:Using NULL would make your code easier to read. Setting a variable to NULL makes it obvious that the variable is a pointer. Setting a variable to 0 would not be as clear.

as if there were any difference between a pointer and an integer of some sort Wink ,
and if something is a variable of non numeric type setting it equal to 0 should fail anyways... unless ofcourse "operator =" was overloaded for that purpose
Quote this message in a reply
Member
Posts: 198
Joined: 2005.01
Post: #11
Hog Wrote:i always use 0 instead of NULL. i don't think NULL is a standard, i've had occurences where it wasn't even defined, and in case someone defines it as something else then things like:

Well, when all else fails... reference USENET Smile

http://www.faqs.org/faqs/C-faq/faq/
Section 5, NULL pointers

NULL is part of the C standard but apparently (I would *never* have guessed this) --

a) You're allowed to use an integer "0" constant instead of NULL in any context (including the "if (!foo)") since NULL is defined by the standard to be "(void *)0", AND

b) The compiler will automagically convert "0" in a pointer context to the magic invalid pointer value required by the system. The only place this will fail is if you pass in something as an int and then convert it to a pointer, and there's not much the compiler can do about knowing about that (however, most systems today do use zero, so it's not a biggie).

So it's not required to be zero, but you can always use zero or NULL. Wow

Cryptic Allusion Games / Cryptic Allusion, LLC
http://www.cagames.com/
Quote this message in a reply
Hog
Member
Posts: 151
Joined: 2002.09
Post: #12
Dan Potter Wrote:a) You're allowed to use an integer "0" constant instead of NULL in any context (including the "if (!foo)") since NULL is defined by the standard to be "(void *)0"

yeah but:

"Using NULL is a stylistic convention only; the preprocessor turns NULL back into 0 which is then recognized by the compiler, in pointer contexts, as before. In particular, a cast may still be necessary before NULL (as before 0) in a function call argument."
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #13
Hog Wrote:yeah but:

"Using NULL is a stylistic convention only..."
That is correct. However, please note that it is the *standard* stylistic convention used to denote an uninitialized address. 95% of programmers out there will mentally tag your code as being non-standard if you use zero instead of NULL. Your call dude.

My personal attitude is that I'm already going against the flow by trying to develop on a Mac. No sense in pouring any more fuel on the fire.
Quote this message in a reply
Member
Posts: 41
Joined: 2005.02
Post: #14
AnotherJake Wrote:My personal attitude is that I'm already going against the flow by trying to develop on a Mac.

That's only until they see the light. Once they see it, they will know the power of the dark side.
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #15
To make your code more readable, I'd suggest the following when it comes to zeros:

When referring to a pointer, use NULL.

When referring to boolean expression, use false (or NO in obj-C).

When referring to a return value, use a defined constant such as noErr or the likes (whatever is appropriate for what you're working with).

If, and only if, you really do mean zero - as in the quantity - then it's ok to use 0.

Also, avoid stuff like while(!someVariable). Writing something like while(someVariable != NULL) conveys semantics much more effectively.

But hey... that's just my opinion Wink
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  C: Global Variables versus Parameters Lizard Man 10 6,122 Jan 13, 2010 08:22 PM
Last Post: Lizard Man
  Thread-Safe reference counted smart pointers in c++ JeroMiya 1 3,268 Nov 21, 2006 12:28 PM
Last Post: OneSadCookie
  Should global variables be pointers or full objects? ia3n_g 1 2,402 Aug 4, 2006 05:53 PM
Last Post: OneSadCookie
  Dangling Pointers/ Memory Leak definitions... WhatMeWorry 1 4,035 Oct 3, 2005 10:43 AM
Last Post: OneSadCookie
  reading void pointers Duane 16 6,858 May 10, 2005 03:47 PM
Last Post: Puzzler183