Is a function called with a NULL pointer undefined?

Possible duplicate:
When does a member function call on an null instance as a result of executing undefined?
C ++ standard: dereferencing a NULL pointer to get a link?

Let's say I have a class:

class A { public: void foo() { cout << "foo"; } }; 

and call foo like this:

 A* a = NULL; a->foo(); 

I suspect this causes undefined behavior, as it is equivalent to (*a).foo() (or is it?), And dereferencing a NULL is UB, but I cannot find the link, Can someone help me? Or is it defined?

No, the function is not virtual . No, I do not get access to any members.

EDIT: I voted to close this question, but I won’t delete it, because I couldn’t find the duplicate myself, and I suspect that this title may be easier to find for others.

+7
source share
4 answers

I am looking for a link that says a->x equivalent to (*a).x .

Here he is:

[C++11: 5.2.5/2]: For the first parameter (period), the first expression must have the full class type. For the second option (arrow), the first expression should have a pointer to the full type of the class. The expression E1->E2 converted to the equivalent form (*(E1)).E2 ; , the rest of 5.2.5 addresses only the first parameter (dot). In any case, the id expression must indicate a member of the class or one of its base classes. [Note: since the class name is inserted into the scope of its class (section 9), the class name is also considered a nested member of this class. -end note] [Note: 3.4.5 describes how names are looked after statements . and -> . -end note]

There is no direct quote for dereferencing a NULL pointer that is UB, unfortunately. You can find more in this question: When does a member function call on a null instance as a result of executing undefined?

+9
source

I know at least one case when this idiom is not only resolved, but also relied upon: Microsoft's MFC class CWnd provides a GetSafeHwnd member function that checks if this==NULL and returns without access to any member variables .

Of course, there are many people who claim that MFC is a very bad example.

Regardless of whether the behavior is undefined or not, in practice this is unlikely to behave badly. The compiler will treat a->foo() as A::foo(a) , which does not dereference the call site if foo not virtual.

+1
source

Yes, this is UB, since a not initialized to point to a valid memory location before dereferencing it.

0
source

Described here: http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#232

At least a few places in the IS state that direct through a null pointer produce undefined behavior: 1.9 [intro.execution] in clause 4 gives "dereferencing a null pointer" as an example undefined and 8.3.2 [dcl.ref], clause 4 (in a note) uses this supposedly undefined behavior as an excuse for the non-existence of “null references”.

However, clause 5.3.1 [expr.unary.op], which describes the unary Operator "*", does not say that the behavior is undefined if the operand is a null pointer, as one would expect. In addition, at least one pass gives the dereference of the correct behavior of the null pointer: 5.2.8 [expr.typeid], paragraph 2, says

If the lvalue expression is obtained by applying the unary * operator to the pointer and the pointer by the null value of the pointer (4.10 [conv.ptr]), the typeid expression throws a bad_typeid exception (18.7.3 [bad.typeid]).

It is incompatible and needs to be cleaned.

Read more in the link if you want to know more.

0
source

All Articles