Proper Use of NULL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cppaddict

    Proper Use of NULL

    Hi,

    A quick question regarding the use of NULL.

    I have a Reader class, which reads from a Display. Reader has two
    relevant members for this question:

    CharsetCollecti on
    -- a collection of possible Charsets being used by Display
    activeCharset
    -- the actual Charset being used by Display

    The first time a Reader object invokes its findNextChar() method, it
    will search every character in each of its Charsets and try to find a
    match for the character at the current cursor position in Display. It
    will then set activeCharset to the appropriate Charset based on the
    search results, so that on future searches only that Charset will be
    searched.

    Right now, I have the constructor of Reader setting
    activeCharset(N ULL) in its initialization list.

    Then my findNextChar() method looks like this:

    char Reader::findNex tChar() {
    if (activeCharset == NULL) { //compiler errors here
    search all Charsets
    set activeCharset based on search results
    } else {
    search only activeCharset
    }
    }

    My question is: What is the right way to do what I am trying to do.
    Am I simply using NULL incorrectly? Is my design on the wrong track
    altogether?

    thanks for any ideas,
    cpp
  • Howard

    #2
    Re: Proper Use of NULL


    "cppaddict" <hello@hello.co m> wrote in message
    news:672qa0haru hufh6363t4hlsk3 ocd6v9n9g@4ax.c om...[color=blue]
    > Hi,
    >
    > char Reader::findNex tChar() {
    > if (activeCharset == NULL) { //compiler errors here
    > search all Charsets
    > set activeCharset based on search results
    > } else {
    > search only activeCharset
    > }
    > }
    >
    > My question is: What is the right way to do what I am trying to do.
    > Am I simply using NULL incorrectly? Is my design on the wrong track
    > altogether?
    >[/color]

    You don't say what the specific error is, or give us the declaration of
    activeCharset, so we can't say for sure. But, if activeCharset is a
    pointer, you can use nil. If it's an integer, you can use 0. (The term
    NULL, I think, is not part of the standard, and is often defined as 0.)
    -Howard



    Comment

    • bartek

      #3
      Re: Proper Use of NULL

      cppaddict <hello@hello.co m> wrote in
      news:672qa0haru hufh6363t4hlsk3 ocd6v9n9g@4ax.c om:
      [color=blue]
      > My question is: What is the right way to do what I am trying to do.
      > Am I simply using NULL incorrectly? Is my design on the wrong track
      > altogether?[/color]

      NULL is zero (0) in C++. Should be used with pointers only.
      Is activeCharset a pointer?

      All in all, it's better to use a plain zero (0) instead of NULL. Less
      confusion, etc.

      Besides, NULL is a preprocessord definition, and as we all know,
      preprocessor is evil. period.

      On the other hand... Are the guys behind Boost.Preproces sor from hell? huh?
      If the preprocessor alone is evil, then that library is the purest, most
      condensed, archetypal evil to walk the earth, soon reaching for critical
      mass! :)

      But I digress...

      Cheers.

      Comment

      • Alan Johnson

        #4
        Re: Proper Use of NULL

        Howard wrote:
        [color=blue]
        > "cppaddict" <hello@hello.co m> wrote in message
        > news:672qa0haru hufh6363t4hlsk3 ocd6v9n9g@4ax.c om...
        >[color=green]
        >>Hi,
        >>
        >>char Reader::findNex tChar() {
        >>if (activeCharset == NULL) { //compiler errors here
        >>search all Charsets
        >>set activeCharset based on search results
        >>} else {
        >>search only activeCharset
        >>}
        >>}
        >>
        >>My question is: What is the right way to do what I am trying to do.
        >>Am I simply using NULL incorrectly? Is my design on the wrong track
        >>altogether?
        >>[/color]
        >
        >
        > You don't say what the specific error is, or give us the declaration of
        > activeCharset, so we can't say for sure. But, if activeCharset is a
        > pointer, you can use nil. If it's an integer, you can use 0. (The term
        > NULL, I think, is not part of the standard, and is often defined as 0.)
        > -Howard
        >
        >
        >[/color]

        The standard says that NULL exists, but is implementation-defined.

        Section 18.1, line 4 :

        "The macro NULL is an implementation-defined C + + null pointer constant
        in this International Standard (4.10)."

        It also mentions that it is defined in <cstddef>.

        Section 4.10 starts by defining a "null pointer constant" :

        "A null pointer constant is an integral constant expression (5.19)
        rvalue of integer type that evaluates to zero."


        Alan


        Alan

        Comment

        • John Harrison

          #5
          Re: Proper Use of NULL

          >[color=blue]
          > You don't say what the specific error is, or give us the declaration of
          > activeCharset, so we can't say for sure. But, if activeCharset is a
          > pointer, you can use nil. If it's an integer, you can use 0. (The term
          > NULL, I think, is not part of the standard, and is often defined as 0.)
          > -Howard
          >[/color]

          Wrong way round, NULL is standard, nil is not.

          john


          Comment

          • Howard

            #6
            Re: Proper Use of NULL


            "John Harrison" <john_andronicu s@hotmail.com> wrote in message
            news:2h4mpkF8qr niU1@uni-berlin.de...[color=blue][color=green]
            > >
            > > You don't say what the specific error is, or give us the declaration of
            > > activeCharset, so we can't say for sure. But, if activeCharset is a
            > > pointer, you can use nil. If it's an integer, you can use 0. (The term
            > > NULL, I think, is not part of the standard, and is often defined as 0.)
            > > -Howard
            > >[/color]
            >
            > Wrong way round, NULL is standard, nil is not.
            >
            > john
            >
            >[/color]

            D'OH! :-)



            Comment

            Working...