NULL macro vs. 0 as null pointer?

F

Francis Abraham

There is no advantage to use a constant rather than a meaningful macro
defined for it.

Instead, macros will give you an idea about what the constant stands for in
that context.
 
K

kk_oop

After reading all the responses, I'm still not understanding what Stroustrup's means when he says (section
5.1.1, The C++ Programming Language):

"In C, it has been popular to define a macro NULL to represent the zero pointer. Because of C++'s tighter type checking, the use of
plain 0, rather than any suggested NULL macro, leads to fewer problems."

One thing I have learned is that "0" in a pointer context means
something very specific to an ANSI C++ compiler. It will cause the
compiler to generate the target machine's version of a "null pointer",
whatever that may be (which may or may not be 0). But if a NULL macro
is set to 0, I just don't understand why Stroustrup would have a problem
with it. He does go on to recommend that if NULL must be used, it
should be a const instead of a macro, e.g., const int NULL = 0, so that
NULL can't be redefined. Perhaps the answer is there. If you use a
macro, some third party include file might redefine null to be something
like (void*)0, which would result in unexpected C++ behavior. Still,
even if that happened, wouldn't such use of the redefined NULL cause a
compiler error in C++ if you tried to assign it to a pointer that was
not type (void *)? Maybe the risk is if you use NULL in a condition
check, e.g., "if somePointer == NULL", instead of getting a compile
error, you just would not get TRUE if the pointer was indeed the null
pointer, so your program would compile, but not run as expected?.

Any thoughts?

Thanks,

Ken
 
V

Victor Bazarov

kk_oop said:
After reading all the responses, I'm still not understanding what
Stroustrup's means when he says (section
5.1.1, The C++ Programming Language):
[...]

Perhaps you should read more. Go to http://groups.google.com/ and
search for "0 vs NULL pointer" in all the comp.lang.c++.* newsgroups.

The subject has been discussed to death, and there is no really any
reason to do it again. If you don't understand it, _you_ need to
make an effort to first exhaust all available sources of information
and only then post again. Please do so.

V
 
K

kk_oop

Victor said:
After reading all the responses, I'm still not understanding what
Stroustrup's means when he says (section

5.1.1, The C++ Programming Language):
[...]

Perhaps you should read more. Go to http://groups.google.com/ and
search for "0 vs NULL pointer" in all the comp.lang.c++.* newsgroups.

The subject has been discussed to death, and there is no really any
reason to do it again. If you don't understand it, _you_ need to
make an effort to first exhaust all available sources of information
and only then post again. Please do so.

V
With all due respect, the responses to my question and varying opinions
seem to indicate that this subject is far from having concensus,
regardless of what prior posts may be out there. Also, the number of
thoughtful responses my post received justifies the asking of the
question. Feel free to just not respond to posts that are of no
interest to you and let others post/respond as they see fit.

And, of course, have a nice day. :)

Ken
 
V

Victor Bazarov

<<
V
With all due respect, the responses to my question and varying opinions seem
to indicate that this subject is far from having concensus, regardless of
what prior posts may be out there. Also, the number of thoughtful responses
my post received justifies the asking of the question. Feel free to just
not respond to posts that are of no interest to you and let others
post/respond as they see fit.

And, of course, have a nice day. :)

Ken
Ken,

Are you looking for consensus regarding what BS meant when he said
that he prefers using 0 instead of NULL? Nobody but BS truly knows
the answer to that. You can e-mail him directly, if you didn't know.

The current "consensus" is that there will be a new keyword in C++
relatively soon: 'nullptr', which should take away most (if not all)
of misunderstanding NULL and 0 produce. I can see how it might be
that you can't wait, but nobody can do anything about it. Any
perpetuation of the NULL vs 0 discussion is only useful when it
somehow contributes to the resolution of the issue. Otherwise it
is nothing but waste of bandwidth. That's just my opinion, of course.

Oh, and feel free to refrain from telling me how to behave myself in
an unmoderated forum. I post as I see fit (just like anybody else
here) and if I see fit to recommend reading the archives, I will, no
matter what you think about my interest to other posts. Got it?

And, of course, have a nice day.

V
 
K

kk_oop

Victor said:
<<
V


Otherwise it
is nothing but waste of bandwidth.
Apparently you consider harrassing someone with a reasonable question to
be a good use of bandwidth?
Oh, and feel free to refrain from telling me how to behave myself in
an unmoderated forum.
Isn't that what you were doing to me?
I post as I see fit (just like anybody else
here) and if I see fit to recommend reading the archives, I will, no
matter what you think about my interest to other posts. Got it?

Take a chill boss. We're all in this together, and we really all care
about figuring out the same things. That's all I've got to say on about
that, but feel free to harrass me further, since this is, after all, an
unmoderated forum. ;)

See ya,

Ken
 
K

Kelsey Bjarnason

[snips]
I think in C NULL is defined as 0. In C++ it is (void*)0.

If that were the case, you'd need an explicit cast in C++ to assign NULL
to any non-void pointer - which doesn't appear to be the case.

In _C_ you can get away with using either 0 or (void *)0; in C++ you'd
need to simply use 0.
 
K

kk_oop

Hi. For anyone still interested, here's my humble conclusion (not thee
conclusion, just my conclusion :) ):

Eventually C++ will have a constant called nullptr. At that time,
that's the thing to use for null pointer values.

Until then, 0 is preferred over the NULL macro since some third party
library could easily provide a NULL macro under the sheets that sets
NULL to (void *)0, as per C. This could inadvertently redefine one's
own NULL that is set to 0. One would hope that a "macro redefinition"
warning would occur in that case, but even if it does, confusion is
bound to occur. Regardless of the macro redef warning, in C++, a type
error would occur if (void *)0 snuck in and was set to a type other than
(void *). I think that's what BS was referring to in regards to C++ and
type checking. Using the constant 0 solves these problems.

Note that BS's recommendation to use const int NULL = 0 doesn't work.
If a NULL macro is being included (as it will be under the sheets in
most cases), the preprocessor will change const int NULL = 0 to
something like"const int 0 = 0" or "const int (void *)0 = 0", neither of
which compiles.

Also, according to the C++ standard, when 0 is set into a pointer, it
does not result in address 0 being generated in the object code. It
actually will result in the target system's null pointer value to be
generated, which may or may not be 0. So according to the C++ standard,
the constant 0, when used as a pointer rvalue, is really an abstraction
for null pointer.

Lastly, when using 0 as a null pointer, one must heed Steve Meyers
recommendation to not allow the same method to be overloaded with an
integer and a pointer. If you do that, programmer confusion will occur
if a 0 constant is used as a parameter (the C++ program will not be
confused--it will assume an integer--but the programmer will easily be
confused by the resulting behavior if he were assuming the parameter to
be treated as a null pointer).

Thanks to all for your varied and interesting thoughts. It's what makes
these newsgroups invaluable. :)

See ya,

Ken
 
M

Mabden

Default User said:
What about a system where 0 is a perfectly valid memory location? Then 0
isn't precisely anything. It's a convention, and a convenient one.


Uuuuhhh... it's not. Ever. In C.

But I am in the camp that sides with (void *)0. NULL should be a pointer.
You are assigning to a pointer, so it should be assigned _as_ a pointer. Not
as an integer. Not as Zero.

char *p = NULL;

To me, that says set pointer P to be a null pointer. IOW, not just pointing
off into space, but to a location that means "not yet assigned".

if (p == NULL)
something(p);

To me, that says "if the pointer P is not assigned, do something to p".
This lets me know that P has not yet been "pointed" to a valid location.

if (*p == 0)
something(p);

To me, that says "if the pointer P's value is 0, do something to p".

I would assume in the second case that P was pointing to a valid location
that happened to have a zero in it.

It's a subtle difference, but the best bugs are subtle. It's better to
prevent misunderstandings by being explicit, especially amongst a group of
programmers.

I hope I said what I mean correctly, if not please, no flames.
 
V

Victor Bazarov

Mabden said:
Uuuuhhh... it's not. Ever. In C.

But I am in the camp that sides with (void *)0. NULL should be a pointer.

You're in a C++ newsgroup, if you didn't notice.
 
M

Mabden

Victor Bazarov said:
pointer.

You're in a C++ newsgroup, if you didn't notice.

WTF?!! No I didn't, honestly. Was this cross-posted at some point?
Oh well, I still disagree with the Great BS, himself, and prefer a pointer
to point to a pointer. NULL should be (void *)0. Until another construct
comes along that assures that:
int i = NULL;
is not a valid statement.

So there. ;-p
 
K

kk_oop

See responses below...
Uuuuhhh... it's not. Ever. In C.
Hi. Here's an excerpt from a useful link provided by a poster in this
thread:

http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1488.pdf

<start of excerpt>
The current C++ standard provides the special rule that 0 is both an
integer constant and a null pointer constant. From [C++03] clause 4.10:

A null pointer constant is an integral constant expression (expr.const)
rvalue of integer type that evaluates to zero. A null pointer constant
can be converted to a pointer type; the result is the null pointer value
of that type and is distinguishable from every other value of pointer to
object or pointer to function type.
<end of excerpt>

So in C++, using 0 for the NULL pointer is more than just a convention.
It is part of the language definition. Note that C is quite different.
From the same article:

<start of excerpt>
The C standard [C99] says (clause 6.3.2.3): An integer constant
expression with the value 0, or such an expression cast to type void *,
is called a null pointer constant.[55] If a null pointer constant is
converted to a pointer type, the resulting pointer, called a null
pointer, is guaranteed to compare unequal to a pointer to any object or
function.
<end of excerpt>

The C++ definition of null pointer does not include (void *)0, whereas
the C definition does.

I hope that helps--without flames :).

See ya,

Ken
 
K

kk_oop

Mabden said:
WTF?!! No I didn't, honestly. Was this cross-posted at some point?
I hope Victor sees it in his heart to forgive this transgression. :)
Oh well, I still disagree with the Great BS, himself, and prefer a pointer
to point to a pointer. NULL should be (void *)0. Until another construct
comes along that assures that:
int i = NULL;
is not a valid statement.

So there. ;-p
The problem is that in C++, if you try to set a pointer that is not type
(void *) equal to (void *)0, you will get a compile error saying that
the lvalue and rvalue are incompatible. That would not happen in C.

See ya,

Ken
 
K

Karl Heinz Buchegger

kk_oop said:
I hope Victor sees it in his heart to forgive this transgression. :)

The problem is that in C++, if you try to set a pointer that is not type
(void *) equal to (void *)0,

It could have been made different in the language.
After all when I write:

int* p = 0;

I am assigning an integer to a pointer. Compared to that,
the issue of allowing to assign (or compare) a void pointer
with a constant value of ((void*)0) to assign or compare to
any other pointer type is the far lesser evil. At least the
whole statement then would deal with pointers only and not with
a mix of pointers and integers.

Just my 2 cents.
 
A

Andre Kostur

Oh well, I still disagree with the Great BS, himself, and prefer a

Uh... we really should get a new shorthand for Bjarne... somehow Bjarne's
name wasn't the first thing that came to mind when I saw "BS" (hint: the B
referred to a male cow.... :) )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top