Re: C Standard Regarding Null Pointer Dereferencing



On Jul 22, 5:09 am, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:

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

This reference causes me to wonder if you actually read the original
newsgroup posting. For some reason, I get the impression that few
responders actually did. I realize it was a long one, and there's
only so much time in a day. I brought the question up because I
believe that either:
1. The standard needs to be addressed due to an ambiguity, should it
be the case that it has not been already, XOR
2. There is no undefined behaviour


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

Yet another item referenced in the original post. Look at your
previous reference then look at this one. Where is a null pointer
value assigned? It's not. Yet the cast expression (the operand)
'(char *)0' _has_ a type, so the result of applying '*' _has_ a type.
That is all that 'sizeof' requires. That is enough for a void
expression. It is enough for the '.' postfix operator. No _value_ is
required in any of those three contexts. It would not be enough for
an assignment or a comparison.


Therefore, using * on a null pointer invokes UB.

Using '*' on a pointer that has been assigned an invalid value is UB.
'(char *)0' is not an lvalue (no cast expression is), hence it cannot
be assigned a value. It is a pointer. It is a null pointer. It is
not a pointer that has been assigned a null pointer value.

I do continue to value your feedback and am hopeful that you or
another responder may pinpoint a definitive reason for UB. So far,
Tim's suggestion that "the wording is imprecise" strikes me as most
likely, iff there really is undefined behaviour.
.



Relevant Pages

  • Re: ANSI C syntax ?
    ... In C assignment is an expression and therefore evaluate to the ... the left operand has qualified or unqualified arithmetic type and ... and the type pointed to by the left has all the qualifiers of the type ... one operand is a pointer to an object or incomplete type and the ...
    (comp.lang.c)
  • C Standard Regarding Null Pointer Dereferencing
    ... "If the operand has type 'pointer to type', ... But compare with and, which do describe evaluation, albeit ... There is no assignment here. ...
    (comp.lang.c)
  • Re: calculating length of an substring
    ... pointer from a pointer. ... arithmetic type and the right has arithmetic type; ... In simple assignment, ... operand is converted to the type of the assignment ...
    (comp.lang.c)
  • Re: convert 16 bit port to Char
    ... Conversions that involve pointers (other than as permitted by the ... is a pointer to a qualified or unqualified version of void ... ... - the left operand is a pointer and the right is a null pointer constant ... In simple assignment, the value of the right operand is converted to the type of the assignment expression and replaces the value stored in the object designated by the left operand. ...
    (comp.arch.embedded)
  • Re: Adding gpib support to Matlab on Linux ?
    ... gpib.c:219: warning: assignment makes pointer from integer without a cast ...
    (comp.soft-sys.matlab)