The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

C++ NULL vs. Zero

I've always been annoyed by the C++ test for new failure as being a test against 0 rather than NULL.

I've always liked the fact that NULL was ((void*)0), not just 0.

Testing against 0 is like leaving off the units - zero what? Zero is an integer, not a pointer.

So what is the advantage of testing against 0 rather than NULL that C++ considers to be an advantage?

hoser Send private email
Wednesday, August 31, 2005
NULL was always 0. If I ever find the original K&R C I have hiding somewhere, I could give you the exact reference.

See [ ] section 5.4, or [ ] which is less formal.
Ori Berger
Wednesday, August 31, 2005
What "C++ test for new failure" are you referring to? What is stopping you from comparing to NULL?
Anon and anon
Wednesday, August 31, 2005
> So what is the advantage of testing against 0 rather than NULL that C++ considers to be an advantage?

I suppose the convention re. NULL is because C++ enforces strong typing, for example ...

void foo(char* p) { }

void test()
  foo(0); //this compiles
  foo((char*)0); //this compiles too
  foo((void*)0); //this doesn't compile: illegal attempt to pass void* to a char* parameter

... and so if you want to be able to for example pass NULL as a parameter to foo, this only works if NULL is #defined as 0 instead of being #defined as (void*) 0.
Christopher Wells Send private email
Wednesday, August 31, 2005
"Testing against 0 is like leaving off the units - zero what?"

0 kilometers = 0 miles
0 liters = 0 gallons
0 ducks = 0 pigeons
Almost H. Anonymous Send private email
Wednesday, August 31, 2005
I knew that "ducks" was the SI unit for unspecified birds. :-)
Andrew J. Brehm Send private email
Wednesday, August 31, 2005
Why "type" of zero matters:

0 bills 
0 income
0 heartbeats
0 ducks
0 lbs/sq inch air pressure

Yes, they are all zero, but the absence of something can be a very important thing.  Its the "type" of zero that matters.

no air != no ducks.

I live many days of my life with no ducks.

When you look at code later that tests for zero, you might have some evil bastard who decided that "well its an int, so I'll return -1 if some such thing happened"
Michael Johnson Send private email
Wednesday, August 31, 2005
> Its the "type" of zero that matters.

I guess they *could* have said that '0' only applies to various kinds of number, and then invented a new keyword 'null' for null pointers.

I expect the reason why they didn't do that was because that would have broken a lot of 'legacy' code (a lot of pain) for insufficient gain (as it is, programmers *can* mostly live with 0 for a null pointer value, even if it does make them go "Hunh?") ... remember that a C++ design goals was that it should be able to compile all sufficiently-polite C code.
Christopher Wells Send private email
Wednesday, August 31, 2005
Actually, new is supposed to throw an exception when it fails, not return null. The only problem is that one of the most popular compilers (MSVC++6) gets that wrong.
comp.lang.c refugee
Wednesday, August 31, 2005
This is what Stroustrup says:

Should I use NULL or 0?

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.
Wednesday, August 31, 2005
0 Fahrenheit != 0 Celcius != 0 Kelvin.

Sorry, had to do it.
Wednesday, August 31, 2005
I am a #define fanatic & use magic numbers less than 1:1000 times.  But '0' is the one magic number that I repeatedly use.  However, I always almost always qualify the zero for each specific instance & context.

Eliminate the confusion as to what NULL is #define'd as & how you should use it by just not using NULL -- use '0' & qualify it.

Examples :

  val32 = (INT32U  )0;
  pval32 = (INT32U *)0;

  if (pfp32 == (FP32 *)0) {...}

  void foo(CHAR *);
  foo((CHAR *)0);
Ian Johns
Wednesday, August 31, 2005
Many people consider it pedantic, but when you have code that has to run on a CPU where address '0' is valid and accessable and also on a machine where accessing address '0' brings up a big nasty dialog box, it can be handy to distinguish between 'test for invalid pointer' and 'test for address 0'.

'0' is 1 character, 'NULL' is an extra 3 characters, even with the use of the shift key the reduced typing for using '0' doesn't make up for slightly less explicit code.

Wednesday, August 31, 2005
Generally, I don't like magic numbers, I prefer NULL, it makes evident you are working with pointers, it can not hide to the eye easily. Anyway I like too use the 0 implicitally in expressions like "while ( !(++ptr) ) { ... }" , I like this special spiritually union between integers, booleans and pointers.
Ross Sampere Send private email
Wednesday, August 31, 2005
It depends on context, and the compiler does tthe right thing. NULL is not, and has never been, anything other than 0. Code like:

  bool is_eos(const char *c) { return *c == NULL; }

_does_ compile, and so does

  double x = NULL;

And yes, I've seen many such examples in production code.

The compiler and the language do not make any distinction between the literal "0" in pointer context and "NULL" (or in any other context, for that matter) - so it amounts to nothing more than a comment or a variable name.

The "NULL" people would probably prefer to call their index variable "index". I use "0" for the pointer, and "i" for the index.
Ori Berger
Wednesday, August 31, 2005
New cannot ever return 0 (NULL) so this is moot...

(PS. It throws an exception)
Wednesday, August 31, 2005
For the pedants:

NULL is *always* defined as 0, even on machines where 0 is a valid memory address which could be returned by malloc(). NULL and 0 are completely 100% interchangable. This is garanteed by the standards.

On a machine where malloc() can return 0 (eg. Inmos Transputers) the compiler is supposed to check every pointer assignment and do something sensible if you type "p = 0".

(I imagine it's much easier to make malloc() do the decent thing though....)
Wednesday, August 31, 2005
When function has arguments of both pointer and integer types, using NULL instead of 0 makes function calls way more readable. Perhaps it applies in other contexts, where variable name is implicit, too.
Monday, September 05, 2005
It's pure preference. I don't like using needless macros with no semantic significance so I prefer 0.
Mr Jack
Monday, September 05, 2005
Hold it, comrade citizen Andrei. This is the certainty police. We find a long string of "0"s more readable than a crazy mishmash of "0" and "NULL".

Your statement is factually incorrect, but this time we will let you off. In future, equivocate. Thank you.
Wednesday, September 07, 2005

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz