checking if pointer is NULL allowed?

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

    checking if pointer is NULL allowed?

    Hi,
    Just to check, if i set a pointer explicitly to NULL, i'm not allowed
    to dereference it? Why is that, it's not
    like it's pointing to any garbage right? Why else set it to NULL. I can
    remember some instances where
    i did just that and printf reported something like : (null).

    I'm asking because i read somewhere that it is not valid. I thought it
    simply wasn't valid to dereference
    a pointer who is _not_ set to point to anything.

    But i can check if it is NULL or not right ? ( i hope ) :-)

    Rgds,
    -alef

  • Walter Roberson

    #2
    Re: checking if pointer is NULL allowed?

    In article <45b79f43$0$333 $e4fe514c@news. xs4all.nl>,
    atv <alef@xs4all.nl wrote:
    >Just to check, if i set a pointer explicitly to NULL, i'm not allowed
    >to dereference it?
    Right.
    >Why is that, it's not
    >like it's pointing to any garbage right?
    It isn't pointing to anything, so I guess from that point of view
    it isn't pointing to any garbage. ;-)
    >Why else set it to NULL. I can
    >remember some instances where
    >i did just that and printf reported something like : (null).
    >I'm asking because i read somewhere that it is not valid. I thought it
    >simply wasn't valid to dereference
    >a pointer who is _not_ set to point to anything.
    But a NULL pointer -has- been set not to point to anything. The NULL
    pointer is -defined- as not pointing to any valid object. What would
    it mean to dereference it? The situation is philosophically similar
    to dereferencing a void* pointer -- there is no "is" there!
    --
    "No one has the right to destroy another person's belief by
    demanding empirical evidence." -- Ann Landers

    Comment

    • santosh

      #3
      Re: checking if pointer is NULL allowed?

      atv wrote:
      Hi,
      Just to check, if i set a pointer explicitly to NULL, i'm not allowed
      to dereference it?
      You _can_, but the behaviour is undefined, since in C, a NULL pointer
      is guaranteed to not point to any valid object.
      Why is that, it's not
      like it's pointing to any garbage right? Why else set it to NULL.
      Setting an uninitialised pointer to NULL is a good practise. Also some
      functions require a null pointer. Many programming constructs require a
      null pointer as a sentinel value. Many functions return a null pointer
      as an indication of failure.

      <snip>
      I'm asking because i read somewhere that it is not valid. I thought it
      simply wasn't valid to dereference
      a pointer who is _not_ set to point to anything.
      Yes.
      But i can check if it is NULL or not right ? ( i hope ) :-)
      Yes, you can.

      Comment

      • Alef.Veld@gmail.com

        #4
        Re: checking if pointer is NULL allowed?

        Well, if it is not pointing to anything anyway, couldn't my compiler
        just printf out "null". And i swear i thought i have seen this.
        and if i can check for it to be NULL (i'm assuming that's why you set
        it to NULL in the first place, like you said, good practice) how does
        it do this. Does it keep track of memory assignments?

        Comment

        • Jens Thoms Toerring

          #5
          Re: checking if pointer is NULL allowed?

          Alef.Veld@gmail .com wrote:
          Well, if it is not pointing to anything anyway, couldn't my compiler
          just printf out "null". And i swear i thought i have seen this.
          Yes, some libc implemetations (that's where printf() is located)
          check if a pointer passed to printf() is NULL and then print out
          "(null)" instead of crashing. But don't rely on that.
          and if i can check for it to be NULL (i'm assuming that's why you set
          it to NULL in the first place, like you said, good practice) how does
          it do this. Does it keep track of memory assignments?
          Setting uninitialized pointers (or pointers on which you have called
          free()) to NULL is good for _you_ since then can check if a pointer
          is valid or not. When you e.g. have

          char *x = malloc( 10 );
          if ( x == NULL ) {
          exit( 0 );
          }
          free(x);

          you can't see by simply looking at the value of 'x' if it's a pointer
          that points to any memory you own (actually, just checking the value
          of a pointer after a call of free() is forbidden). But if you always
          set such pointer to NULL then _you_ can test if it's a valid pointer
          or not and _you_ can thus avoid using bad pointers (moreover, if the
          pointer ist invalid but not NULL and accidentaly points to some memory
          you can access it might look as dereferencing it works as expected
          while in reality there's a big fat bug in your code).

          Regards, Jens
          --
          \ Jens Thoms Toerring ___ jt@toerring.de
          \______________ ____________ http://toerring.de

          Comment

          • Alef.Veld@gmail.com

            #6
            Re: checking if pointer is NULL allowed?

            that's what i suspected and wanted to hear, thanks :-)

            On Jan 24, 8:05 pm, j...@toerring.d e (Jens Thoms Toerring) wrote:
            Alef.V...@gmail .com wrote:
            Well, if it is not pointing to anything anyway, couldn't my compiler
            just printf out "null". And i swear i thought i have seen this.Yes, some libc implemetations (that's where printf() is located)
            check if a pointer passed to printf() is NULL and then print out
            "(null)" instead of crashing. But don't rely on that.
            >
            and if i can check for it to be NULL (i'm assuming that's why you set
            it to NULL in the first place, like you said, good practice) how does
            it do this. Does it keep track of memory assignments?Set ting uninitialized pointers (or pointers on which you have called
            free()) to NULL is good for _you_ since then can check if a pointer
            is valid or not. When you e.g. have
            >
            char *x = malloc( 10 );
            if ( x == NULL ) {
            exit( 0 );
            }
            free(x);
            >
            you can't see by simply looking at the value of 'x' if it's a pointer
            that points to any memory you own (actually, just checking the value
            of a pointer after a call of free() is forbidden). But if you always
            set such pointer to NULL then _you_ can test if it's a valid pointer
            or not and _you_ can thus avoid using bad pointers (moreover, if the
            pointer ist invalid but not NULL and accidentaly points to some memory
            you can access it might look as dereferencing it works as expected
            while in reality there's a big fat bug in your code).
            >
            Regards, Jens
            --
            \ Jens Thoms Toerring ___ j...@toerring.d e
            \______________ ____________ http://toerring.de

            Comment

            • Richard Tobin

              #7
              Re: checking if pointer is NULL allowed?

              In article <45b79f43$0$333 $e4fe514c@news. xs4all.nl>,
              atv <alef@xs4all.nl wrote:
              >Just to check, if i set a pointer explicitly to NULL, i'm not allowed
              >to dereference it?
              [...]
              >But i can check if it is NULL or not right ? ( i hope ) :-)
              Perhaps you misunderstand "dereferenc e"? You're allowed to test the
              pointer itself:

              if(p == null) /* good */

              but not "follow" it:

              if(*p == 'a') /* bad */

              printf()s that print "(null)" for NULL pointers are doing something
              like:

              if(p == NULL)
              puts("(null)");
              else
              ..whatever..;

              -- Richard
              --
              "Considerat ion shall be given to the need for as many as 32 characters
              in some alphabets" - X3.4, 1963.

              Comment

              • Default User

                #8
                Re: checking if pointer is NULL allowed? - TPA

                Alef.Veld@gmail .com wrote:
                that's what i suspected and wanted to hear, thanks :-)
                Please don't top-post. Your replies belong following or interspersed
                with properly trimmed quotes. See the majority of other posts in the
                newsgroup, or:
                <http://www.caliburn.nl/topposting.html >

                Comment

                • Default User

                  #9
                  Re: checking if pointer is NULL allowed?

                  Alef.Veld@gmail .com wrote:
                  Well, if it is not pointing to anything anyway, couldn't my compiler
                  just printf out "null". And i swear i thought i have seen this.
                  It's undefined behavior. There IS NO DEFINED BEHAVIOR for undefined
                  behavior. Say that over and over until you get it. That means that if
                  your implentation wants to crash it can crash. There is no defined
                  behavior for undefined behavior. If your implementation wants to print
                  (null), it can. There is no defined behavior for undefined behavior.

                  PS:

                  There is no defined behavior for undefined behavior.





                  Brian

                  Comment

                  • Ben Pfaff

                    #10
                    Re: checking if pointer is NULL allowed?

                    Alef.Veld@gmail .com writes:
                    Well, if it is not pointing to anything anyway, couldn't my compiler
                    just printf out "null". And i swear i thought i have seen this.
                    Are you confusing dereferencing an invalid pointer with passing a
                    null pointer to printf in place of a pointer argument? Both are
                    undefined behavior. The former is usually difficult or expensive
                    for an implementation to detect, whereas the latter is handled
                    gracefully by better-quality C libraries (which often do print
                    "null" or "(null)").
                    --
                    "In My Egotistical Opinion, most people's C programs should be indented six
                    feet downward and covered with dirt." -- Blair P. Houghton

                    Comment

                    • Ian Collins

                      #11
                      Re: checking if pointer is NULL allowed?

                      Ben Pfaff wrote:
                      Alef.Veld@gmail .com writes:
                      >
                      >
                      >>Well, if it is not pointing to anything anyway, couldn't my compiler
                      >>just printf out "null". And i swear i thought i have seen this.
                      >
                      >
                      Are you confusing dereferencing an invalid pointer with passing a
                      null pointer to printf in place of a pointer argument? Both are
                      undefined behavior. The former is usually difficult or expensive
                      for an implementation to detect, whereas the latter is handled
                      gracefully by better-quality C libraries (which often do print
                      "null" or "(null)").
                      That depend on one's idea of graceful. Passing a null pointer as a
                      parameter to printf is never, a good thing to do.

                      --
                      Ian Collins.

                      Comment

                      • =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=

                        #12
                        Re: checking if pointer is NULL allowed?

                        Ian Collins wrote:
                        Ben Pfaff wrote:
                        Alef.Veld@gmail .com writes:

                        >Well, if it is not pointing to anything anyway, couldn't my compiler
                        >just printf out "null". And i swear i thought i have seen this.

                        Are you confusing dereferencing an invalid pointer with passing a
                        null pointer to printf in place of a pointer argument? Both are
                        undefined behavior. The former is usually difficult or expensive
                        for an implementation to detect, whereas the latter is handled
                        gracefully by better-quality C libraries (which often do print
                        "null" or "(null)").
                        >
                        That depend on one's idea of graceful. Passing a null pointer as a
                        parameter to printf is never, a good thing to do.
                        Never say never :) There's nothing wrong with printf("%p", (void *) 0);

                        Comment

                        • Bob Martin

                          #13
                          Re: checking if pointer is NULL allowed?

                          in 716374 20070124 230752 Ben Pfaff <blp@cs.stanfor d.eduwrote:
                          >Alef.Veld@gmai l.com writes:
                          >
                          >Well, if it is not pointing to anything anyway, couldn't my compiler
                          >just printf out "null". And i swear i thought i have seen this.
                          >
                          >Are you confusing dereferencing an invalid pointer with passing a
                          >null pointer to printf in place of a pointer argument? Both are
                          >undefined behavior. The former is usually difficult or expensive
                          >for an implementation to detect, whereas the latter is handled
                          >gracefully by better-quality C libraries (which often do print
                          >"null" or "(null)").
                          Surely all the OP needed to know is that inspecting the value of a pointer
                          does not constitute "dereferenc ing" it?

                          Comment

                          • jaysome

                            #14
                            Re: checking if pointer is NULL allowed?

                            On 24 Jan 2007 23:07:27 -0800, "Harald van D?k" <truedfx@gmail. com>
                            wrote:
                            >Ian Collins wrote:
                            >Ben Pfaff wrote:
                            Alef.Veld@gmail .com writes:
                            >
                            >
                            >>Well, if it is not pointing to anything anyway, couldn't my compiler
                            >>just printf out "null". And i swear i thought i have seen this.
                            >
                            >
                            Are you confusing dereferencing an invalid pointer with passing a
                            null pointer to printf in place of a pointer argument? Both are
                            undefined behavior. The former is usually difficult or expensive
                            for an implementation to detect, whereas the latter is handled
                            gracefully by better-quality C libraries (which often do print
                            "null" or "(null)").
                            >>
                            >That depend on one's idea of graceful. Passing a null pointer as a
                            >parameter to printf is never, a good thing to do.
                            >
                            >Never say never :) There's nothing wrong with printf("%p", (void *) 0);
                            And oddly enough, there may be something wrong with the following,
                            understandble, mistake:

                            /*let's print the value of the NULL pointer*/
                            printf("%p\n", NULL);

                            --
                            jay

                            Comment

                            • Alef.Veld@gmail.com

                              #15
                              Re: checking if pointer is NULL allowed?

                              Ben Pfaff wrote:
                              Alef.V...@gmail .com writes:
                              >>Well, if it is not pointing to anything anyway, couldn't my compiler
                              >>just printf out "null". And i swear i thought i have seen this.
                              Are you confusing dereferencing an invalid pointer with passing a
                              null pointer to printf in place of a pointer argument?
                              I'm not quite sure what you mean here. I just wanted to know why i
                              sometimes noticed that printf printed out a nice "null" when i gave it
                              a pointer i explicitly set to NULL. I'm not talking about 'bad'
                              pointers here. But now i have my answer, the better libc libraries do
                              print this out. Thanks everyone.

                              Surely all the OP needed to know is that inspecting the value of a
                              pointer
                              does not constitute "dereferenc ing" it?

                              No. I know that :-)

                              And oddly enough, there may be something wrong with the following,
                              understandble, mistake:
                              /*let's print the value of the NULL pointer*/
                              printf("%p\n", NULL);
                              --
                              jay

                              Would this not print out something akin to 0x0?

                              Comment

                              Working...