NULL pointer dereferencing?

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

    #1

    NULL pointer dereferencing?

    Does the standard define what happens when a NULL pointer is dereferenced? If so, where?
  • relative

    #2
    Re: NULL pointer dereferencing?

    no

    Comment

    • Ben Pope

      #3
      Re: NULL pointer dereferencing?

      Angel Tsankov wrote:[color=blue]
      > Does the standard define what happens when a NULL pointer is
      > dereferenced? If so, where?[/color]

      That's probably the classic case of undefined behaviour.

      Ben Pope
      --
      I'm not just a number. To many, I'm known as a string...

      Comment

      • Ron Natalie

        #4
        Re: NULL pointer dereferencing?

        Angel Tsankov wrote:[color=blue]
        > Does the standard define what happens when a NULL pointer is
        > dereferenced? If so, where?[/color]

        It is in the definition of undefined behavior as a matter of
        fact.

        Comment

        • Kaz Kylheku

          #5
          Re: NULL pointer dereferencing?

          Angel Tsankov wrote:[color=blue]
          > Does the standard define what happens when a NULL pointer is dereferenced? If so, where?[/color]

          In some cases the behavior is defined, namely where the expression is
          not actually evaluated.

          #include <cstdlib>

          // ...

          type *p = (type *) malloc(sizeof *p);

          Here p is dereferenced, and doesn't even have a value yet, let alone a
          null one. That's okay, because the sizeof operator only needs the
          expression for its type, not its value. The expression is not
          evaluated.

          By the way, don't call it a NULL pointer. There are null pointers,
          null characters, the ASCII character NUL and the NULL constant.
          Typography matters!

          :)

          There are some shady uses of null pointers which are strictly speaking
          undefined, but are quasi-defined in the sense of having quite broad
          portability.

          You may find a use of a null pointer in the definition of the
          offsetof() macro from ANSI C.

          This is from the comp.lang.c FAQ:

          9.8: How can I determine the byte offset of a field
          within a structure?

          ANSI C defines the offsetof macro, which should be used if available;
          see <stddef.h>. If you don't have it, a suggested implementation is


          #define offsetof(type, mem) ((size_t) \
          ((char *)&((type *) 0)->mem - (char *)((type *) 0)))

          This implementation is not 100% portable; some compilers may
          legitimately refuse to accept it.

          As you can see, there is a dereference of a null pointer in there,
          namely the ((type *) 0)->mem! The value is not used: rather, the
          address of that dereferenced value is taken right away. The address is
          subtracted from the null pointer, to make the construct portable to
          systems where a null pointer does not convert to an integer zero, and
          the result of the subtraction is the offset of that member within the
          struct.

          Pointer arithmetic on null pointers is also undefined.

          Comment

          • Greg

            #6
            Re: NULL pointer dereferencing?

            Angel Tsankov wrote:[color=blue]
            > Does the standard define what happens when a NULL pointer is dereferenced? If so, where?[/color]

            The Standard leaves the behavior undefined in this case.

            However, nearly every implementation does define the behavior (which an
            implementation is free to do) - usually as a memory access violation.

            Greg

            Comment

            Working...