Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Followup: NULL macro vs. 0 as null pointer?

Reply
Thread Tools

Followup: NULL macro vs. 0 as null pointer?

 
 
Ken
Guest
Posts: n/a
 
      07-23-2004
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-23-2004
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
 
Reply With Quote
 
 
 
 
jeffc
Guest
Posts: n/a
 
      07-23-2004

"Ken" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.


 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      07-23-2004

Zzzzzzzzzzz



-JKop
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      07-24-2004
On Fri, 23 Jul 2004 12:17:42 -0400, Victor Bazarov
<(E-Mail Removed)> 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
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-24-2004
"Jack Klein" <(E-Mail Removed)> wrote...
> On Fri, 23 Jul 2004 12:17:42 -0400, Victor Bazarov
> <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
in S.E. Asia : Canon EOS 300d with 100 macro ED vs. Nikon D70 with Nikon 105 macro ? J. Cod Digital Photography 0 09-29-2004 05:46 AM
NULL macro vs. 0 as null pointer? Ken C++ 69 07-31-2004 04:45 AM
#define macro to enclose an older macro with strings Dead RAM C++ 20 07-14-2004 10:58 AM
macro name from macro? D Senthil Kumar C Programming 1 09-21-2003 07:02 PM



Advertisments