Followup: NULL macro vs. 0 as null pointer?

K

Ken

Hi. This is a followup to my NULL macro vs. 0 question. It's in a
separate thread, because I need to post using google, so my original
thread isn't available to me yet :(.

Anyway, I just noticed that in Stroustrup's book, 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."

Could someone explain how the tighter type checking plays a role, and
what problems are being avoided?

Thanks again,

Ken
 
V

Victor Bazarov

Ken said:
Hi. This is a followup to my NULL macro vs. 0 question. It's in a
separate thread, because I need to post using google, so my original
thread isn't available to me yet :(.

Anyway, I just noticed that in Stroustrup's book, 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."

Could someone explain how the tighter type checking plays a role, and
what problems are being avoided?

Thanks again,

Ken

In C NULL is often defined as (void*)0

In C++ conversion from a pointer to void to any other pointer requires
an explicit cast. So, every time you write something like

class S {
std::set<std::string> *pset;
public:
S();
};

in the initialisation list you'd have to write

S::S() : pset(static_cast<std::set<std::string> >(NULL)) {}

instead of

S::S() : pset(NULL) {}

or, which is even less

S::S() : pset(0) {}

Victor
 
J

jeffc

Ken said:
Hi. This is a followup to my NULL macro vs. 0 question. It's in a
separate thread, because I need to post using google, so my original
thread isn't available to me yet :(.

This is not going to work. You're just going to have to wait and read all
the replies in the other thread (where my reply is.) Otherwise we'll have 6
threads by the end of the day, all on the same question.
 
J

Jack Klein

In C NULL is often defined as (void*)0

In C++ conversion from a pointer to void to any other pointer requires
an explicit cast. So, every time you write something like

And what does the C definition of NULL have to do with anything? The
C++ standard specifically states that NULL may not be defined as
(void*)0 in a conforming C++ implementation? In any standard
implementation, the macro NULL may be assigned to any pointer at any
time without a cast.

This might have been a minor issue in the days when cfront and similar
preprocessors had to interoperate with C supplied headers, but those
days are no more.
 
V

Victor Bazarov

Jack Klein said:
And what does the C definition of NULL have to do with anything?

It only has a little to do with it: since it's a macro, it could be
accidentally redefined by a C header included somewhere not under your
control. That's not the language issue, it's the _problem_ BS is
talking about.

If you do everything the Standard says you should, there is no problem
and the use of NULL is just as fine as the use of 0. I am pretty sure
that's what BS meant when he said what he said. Just read all the
hundreds of messages on the topic posted in comp.lang.c++.* for the
last few years, you'll be able to fish out the right explanation.

Victor
 

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,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top