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.

See 6.5.3.2.

"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 6.3.2.3.)

Therefore, using * on a null pointer invokes UB.

<snip>

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