Victor Bazarov schreef op 23-Dec-13 2:06 PM:
On 12/23/2013 7:57 AM, Wouter van Ooijen wrote:
Victor Bazarov schreef op 23-Dec-13 1:40 PM:
You're dereferencing a null pointer, and that has undefined behavior
(read up on that). Not illegal, just not defined at all. It might do
what you expect (like crash the program due to invalid memory access),
or it might e-mail dirty pictures of your grandma to the Deputy Prime
Minister.
Note that in this example, g is false, hence at run time the integer at
NULL is never accessed. Does the binding of a refernce to the object
pointed to realy counts as dereferencing?
Expression *ptr _is_ dereferencing. And don't remove relevant quoting,
please.
Quoting 5.3.1:
The unary * operator performs indirection: the expression to which it is
applied shall be a pointer to an object type, or a pointer to a function
type and the result is an lvalue referring to the object or function to
which the expression points, if any. If the pointer is a null pointer
value (4.10 [conv.ptr]) or points one past the last element of an array
object (5.7 [expr.add]), the result is an empty lvalue and does not
refer to any object or function. An empty lvalue is not modifiable. If
the type of the expression is “pointer to T,” the type of the result is
“T.” [Note: a pointer to an incomplete type (other than cv void) can be
dereferenced. The lvalue thus obtained can be used in limited ways (to
initialize a reference, for example); this lvalue must not be converted
to an rvalue, see 4.1 [conv.lval].—end note]
Seems to me that this exactly covers the code eat hand.