NULL versus 0 pointers?
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.
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.
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.
Yes, NULL is the standard for an uninitialized address.
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.
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.
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.
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.
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...
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
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:
would certainly cause trouble.
Code:
if(!foo) foo->doFoo();
if(foo) delete foo;would certainly cause trouble.
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
,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
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

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.
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."
Hog Wrote:yeah but: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.
"Using NULL is a stylistic convention only..."
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.
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.
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
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
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| C: Global Variables versus Parameters | Lizard Man | 10 | 4,982 |
Jan 13, 2010 08:22 PM Last Post: Lizard Man |
|
| Should global variables be pointers or full objects? | ia3n_g | 1 | 2,066 |
Aug 4, 2006 05:53 PM Last Post: OneSadCookie |
|
| Dangling Pointers/ Memory Leak definitions... | WhatMeWorry | 1 | 3,483 |
Oct 3, 2005 10:43 AM Last Post: OneSadCookie |
|
| reading void pointers | Duane | 16 | 6,208 |
May 10, 2005 03:47 PM Last Post: Puzzler183 |
|
| Pointers & Archiving in Cocoa | Shivers | 6 | 3,482 |
Apr 4, 2005 02:36 AM Last Post: DoG |
|

