Followup: NULL macro vs. 0 as null pointer?

Discussion in 'C++' started by Ken, Jul 23, 2004.

  1. Ken

    Ken Guest

    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
    Ken, Jul 23, 2004
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Jul 23, 2004
    #2
    1. Advertising

  3. Ken

    jeffc Guest

    "Ken" <> wrote in message
    news:...
    > 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.
    jeffc, Jul 23, 2004
    #3
  4. Ken

    JKop Guest

    Zzzzzzzzzzz



    -JKop
    JKop, Jul 23, 2004
    #4
  5. Ken

    Jack Klein Guest

    On Fri, 23 Jul 2004 12:17:42 -0400, Victor Bazarov
    <> 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/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jul 24, 2004
    #5
  6. "Jack Klein" <> wrote...
    > On Fri, 23 Jul 2004 12:17:42 -0400, Victor Bazarov
    > <> 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
    Victor Bazarov, Jul 24, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Maris

    USB vhdl code (followup)

    Rob Maris, Aug 6, 2004, in forum: VHDL
    Replies:
    3
    Views:
    2,812
    Alex Gibson
    Aug 8, 2004
  2. David Waz...

    Machine.Config followup...

    David Waz..., Jul 3, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    390
    Yan-Hong Huang[MSFT]
    Jul 7, 2003
  3. Ken
    Replies:
    69
    Views:
    12,488
    Michael D. Borghardt
    Jul 31, 2004
  4. aneuryzma
    Replies:
    3
    Views:
    696
    Jim Langston
    Jun 16, 2008
  5. Christopher
    Replies:
    4
    Views:
    431
    Ruben Safir
    Jul 9, 2011
Loading...

Share This Page