Re: C Standard Regarding Null Pointer Dereferencing

Shao Miller wrote:
On Jul 21, 8:44 pm, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
Open and shut case.

Obviously it "feels" like it should be undefined behaviour, since
we've all been trying to avoid the act of a "null pointer dereference"
for a long time.


"The unary * operator denotes indirection. If the operand points to a function, the result is a function designator; if it points to an object, the result is an lvalue designating the object. If the operand has type ‘‘pointer to type’’, the result has type ‘‘type’’. If an
invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined."

NULL is an invalid value - it is guaranteed not to point to any object or function. (See

Therefore, using * on a null pointer invokes UB.


Richard Heathfield <>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within

Relevant Pages

  • Re: If It Were Easy...
    ... Is this actually a part of the C standard, i.e. using the NULL pointer ... " Address and indirection operators ... Among the invalid values for dereferencing a pointer by the unary * ...
  • Re: pointer arithmetic
    ... ram@xxxxxxxxxxxxxxxxxx (Stefan Ram) writes: ... The unary * operator denotes indirection. ... operand has type "pointer to type", ...
  • Re: null references
    ... To say that null and invalid references don't exist suggests that if you would just change to the function would be unable to fail and therefore correctly manages the semaphore, but this is a bogus argument. ... The reference version of the function can still throw an access violation and still needs to be coded for exception safety. ... If you define the above function as taking a reference arg then the undefined behaviour occurs the moment someone tries to dereference an invalid pointer. ...
  • Re: access_ok macor
    ... The problem which I have is that if I run socketpair, getsockname, getpeername LTP ... tests with invalid salen pointer there are addresses close to 0x0. ... This sounds like a classic NULL pointer dereference that is handled correctly ... err = get_user; ...
  • Re: interpreting a null pointer as an empty (null string)
    ... there requires doing something that has undefined behavior (or, at least, ... which is the usual destination of the null pointer. ... Looks like this is explicitly disallowed by the Standard, ... null pointers being "invalid" as operands of the indirection operator; ...