Re: C Standard Regarding Null Pointer Dereferencing
- From: Shao Miller <sha0.miller@xxxxxxxxx>
- Date: Thu, 22 Jul 2010 06:22:24 -0700 (PDT)
On Jul 22, 5:09 am, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
This reference causes me to wonder if you actually read the original
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."
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
Yet another item referenced in the original post. Look at your
NULL is an invalid value - it is guaranteed not to point to any object
or function. (See 6.3.2.3.)
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.
Using '*' on a pointer that has been assigned an invalid value is UB.
Therefore, using * on a null pointer invokes 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.
.
- References:
- C Standard Regarding Null Pointer Dereferencing
- From: Shao Miller
- Re: C Standard Regarding Null Pointer Dereferencing
- From: Richard Heathfield
- Re: C Standard Regarding Null Pointer Dereferencing
- From: Shao Miller
- Re: C Standard Regarding Null Pointer Dereferencing
- From: Richard Heathfield
- Re: C Standard Regarding Null Pointer Dereferencing
- From: Shao Miller
- Re: C Standard Regarding Null Pointer Dereferencing
- From: Richard Heathfield
- C Standard Regarding Null Pointer Dereferencing
- Prev by Date: Re: Reading little-endian data from a file in a portable manner
- Next by Date: Re: Newbie: Problems Picking up integer values (dereferencing problem?) Really Trying To ReadTheGoshDarnedManual
- Previous by thread: Re: C Standard Regarding Null Pointer Dereferencing
- Next by thread: Re: C Standard Regarding Null Pointer Dereferencing
- Index(es):
Relevant Pages
|