By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,265 Members | 1,710 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,265 IT Pros & Developers. It's quick & easy.

Followup: NULL macro vs. 0 as null pointer?

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Ken wrote:
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
Jul 22 '05 #2

P: n/a

"Ken" <kk****@yahoo.com> wrote in message
news:51**************************@posting.google.c om...
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.
Jul 22 '05 #3

P: n/a

Zzzzzzzzzzz

-JKop
Jul 22 '05 #4

P: n/a
On Fri, 23 Jul 2004 12:17:42 -0400, Victor Bazarov
<v.********@comAcast.net> wrote in comp.lang.c++:
Ken wrote:
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


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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #5

P: n/a
"Jack Klein" <ja*******@spamcop.net> wrote...
On Fri, 23 Jul 2004 12:17:42 -0400, Victor Bazarov
<v.********@comAcast.net> wrote in comp.lang.c++:
Ken wrote:
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


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
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.