9

In my early days with C++, I seem to recall you could call a member function with a NULL pointer, and check for that in the member function:

class Thing {public: void x();}

void Thing::x()
{ if (this == NULL) return; //nothing to do
   ...do stuff...
}

Thing* p = NULL; //nullptr these days, of course
p->x(); //no crash

Doing this may seem silly, but it was absolutely wonderful when writing recursive functions to traverse data structures, where navigating could easily run into the blind alley of a NULL; navigation functions could do a single check for NULL at the top and then blithely call themselves to try to navigate deeper without littering the code with additional checks.

According to g++ at least, the freedom (if it ever existed) has been revoked. The compiler warns about it, and if compiling optimized, it causes crashes.

Question 1: does the C++ standard (any flavor) disallow a NULL this? Or is g++ just getting in my face?

Question 2. More philosophically, why? 'this' is just another pointer. The glory of pointers is that they can be nullptr, and that's a useful condition.

I know I can get around this by making static functions, passing as first parameter a pointer to the data structure (hellllo Days of C) and then check the pointer. I'm just surprised I'd need to.

Edit: To upvote an answer I'd like to see chapter and verse from the standard on why this is disallowed. Note that my example at NO POINT dereferences NULL. Nothing is virtual here, and p is copied to "argument this" but then checked before use. No defererence occurs! so dereference of NULL can't be used as a claim of UB.

People are making a knee-jerk reaction to *p and assuming it isn't valid if p is NULL. But it is, and the evidence is here: http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#232 In fact it calls out two cases when a pointer, p, is surprisingly valid as *p: when p is null or when p points one element past the end of an array. What you must never do is USE the value of *p... other than to take the address of it. &*p where p == nullptr for any pointer type p IS valid. It's fine to point out that p->x() is really (*p).x(), but at the end of the day that translates to x(&*p) and that is perfectly well formed and valid. For p=nullptr... it simply becomes x(nullptr).

I think my debate should be with the standards community; in their haste to undercut the concept of a null reference, they left wording unclear. Since no one here has demanded p->x() is UB without trying to demand that it's UB because *p is UB; and because *p is definitely not UB because no aspect of x() uses the referenced value, I'm going to put this down to g++ overreaching on a standard ambiguity. The absolutely identical mechanism using a static function and extra parameter is well defined, so it's not like it stops my refactor effort. Consider the question withdrawn; portable code can't assume this==nullptr will work but there's a portable solution available, so in the end it doesn't matter.

12
  • 4
    this cannot be null in the context of the object...
    – smac89
    Jan 2, 2018 at 20:23
  • 3
    this is a pointer created and defined with every object creation that points to that object - i.e. it contains the address of created object. Why would it be a nullptr if the object was created? What would be the reason to change this to point somewhere else? We highly rely on this pointing at the right memory address
    – Fureeish
    Jan 2, 2018 at 20:23
  • 2
    If invoking a method on an invalid object isn't undefined behaviour I'll be stunned. You may have been getting by in the past by the grace of <insert deity here>. Jan 2, 2018 at 20:24
  • 2
    Those early days (early '80s) were well before the first ISO standard. You used to be able to modify this. The source code of CFront even had a check for this == 0. You can read more about that part here.
    – chris
    Jan 2, 2018 at 20:28
  • 2
    "'this' is just another pointer" I've heard committee members say that if they could go back, they might make it a reference instead. Jan 2, 2018 at 20:32

5 Answers 5

13

To be in a situation where this is nullptr implies you called a non-static member function without using a valid instance such as with a pointer set to nullptr. Since this is forbidden, to obtain a null this you must already be in undefined behavior. In other words, this is never nullptr unless you have undefined behavior. Due to the nature of undefined behavior, you can simplify the statement to simply be "this is never nullptr" since no rule needs to be upheld in the presence of undefined behavior.

9
  • You're right. That answers the question better coming from the UB direction.
    – user0042
    Jan 2, 2018 at 20:34
  • 1
    No, that doesn't make sense. p->x() does not dereference p. It passes p as the first (hidden, implicitly named this) argument to x(), which then might proceed to dereference this in various ways, but my example doesn't. There are ways that p->x() could implicitly dereference something. x() could be virtual, and then something like a vtable would get involved. But my example had nothing virtual. To upvote this I need a reference in the standard to undefined behaviour that makes reference to something other than deferencing nullptr, which my example at no point does.
    – Scott M
    Jan 2, 2018 at 21:58
  • 3
    @user15001 According to [expr.ref] in the standard (checked against N4594), p->x and (*(p)).x must have the same behaviour. The second is clearly dereferencing a null pointer in this case and is bad, so to have the same behaviour the first case must also be bad. Jan 2, 2018 at 22:49
  • @user15001 - You're thinking in terms of how a COMPILER might do things. The C++ standard describes meaning of the code construct and, as pointed out by user15001, the meaning in the standard is that -> does dereferencing. The standard is the yardstick for assessing correctness of implementations (compilers, etc) not the reverse.
    – Peter
    Jan 3, 2018 at 0:09
  • "The second is clearly dereferencing a null pointer". No. If it was then int* p = &*(int*)nullptr; would crash, but instead it's perfectly legal and executes without issue. * converts a pointer to a reference; it only "dereferences" when context demands it.
    – Scott M
    Jan 3, 2018 at 0:15
5

Question 1: does the C++ standard (any flavor) disallow a NULL this? Or is g++ just getting in my face?

The C++ standard disallows it -- calling a method on a NULL pointer is officially 'undefined behavior' and you must avoid doing it or you will get bit. In particular, optimizers will assume that the this-pointer is non-NULL when making optimizations, leading to strange/unexpected behaviors at runtime (I know this from experience :))

Question 2. More philosophically, why? 'this' is just another pointer. The glory of pointers is that they can be nullptr, and that's a useful condition.

I'm not sure it matters, really; it's what is specified in the C++ standard, and they probably had their reasons (philosophical or otherwise), but since the standard specifies it, the compilers expect it, therefore as programmers we have to abide by it, or face undefined behavior. (One can imagine an alternate universe where NULL this-pointers are allowed, but we don't live there)

1
  • 1
    For question2, I think it is historic: this has been introduced before reference has...
    – Jarod42
    Jan 2, 2018 at 21:46
0

The question has already been answered - it is undefined behavior to dereference a null pointer, and using *obj or obj-> are both dereferencing.

Now (since I assume you have a question on how to work around this) the solution is to use static function:

class Foo {
    static auto bar_st(Foo* foo) { if (foo) return foo->bar(); }
}

Having said that, I do think that gcc's decision of eliminating all branches for nullptr this was not a wise one. Nobody gained by that, and a lot of people suffered. What's the benefit?

10
  • I suppose one benefit might be that a lot of people learned (albeit the hard way) that the C++ specification doesn't allow you to use null this-pointers... Jan 2, 2018 at 22:58
  • The benefit is for people who took care up front, and ensured they never dereferenced a NULL pointer to call a non-static member function. They don't suffer the performance hit of multiple checks (at least one on every call of a member function) for a situation they have prevented. The only people who "suffer" would be those who write code with undefined behaviour due to bad technique.
    – Peter
    Jan 2, 2018 at 23:53
  • There are some circumstances where *p (if p is a null pointer) is allowed; p->bar() is not one of those of course
    – M.M
    Jan 3, 2018 at 2:42
  • @M.M do you mean unevaluated contexts (such as decltype(*p))?
    – SergeyA
    Jan 3, 2018 at 15:12
  • @Peter people who would not have used this technique, wouldn't have a branch either, so they did not benefit in any way.
    – SergeyA
    Jan 3, 2018 at 15:13
0

C++ does not allow calling member functions of null object. Objects need identity and that can not be stored to null pointer. What would happen if member function would read or write a field of a object referenced by null pointer?

It sounds like you could use null object pattern in your code to create wanted result.

Null pointer is recognised a problematic entity in object oriented languages because in most languages it is not a object. This creates a need for code that specifically handles the case something being null. While checking for special null pointer is the norm. There are other approaches. Smalltalk actually has a NullObject which has methods its own methods. As all objects it can also be extended. Go programming language does allow calling struct member functions for something that is nil (which sounds like something required in the question).

2
  • C++ allows calling member function in a constructor. The object is not considered initialised until the constructor is completed.
    – Peter
    Jan 2, 2018 at 23:55
  • That is true. But this is no longer null. I have to find better word for the situation.
    – Panu
    Jan 3, 2018 at 7:10
-3

this might be null too if you delete this (which is possible but not recommended)

3
  • 2
    'delete this' won't change the value of the this-pointer; rather it will make the this-pointer into a dangling pointer which is unsafe to use. Jan 2, 2018 at 21:36
  • Using the value of a deleted pointer causes implementation-defined behaviour since C++14 (was UB prior to that), so the implementation might define that such a pointer behaves like a null pointer.
    – M.M
    Jan 2, 2018 at 22:35
  • Stroustrup says: "C++ explicitly allows an implementation of delete to zero out an lvalue operand, and I had hoped that implementations would do that, but that idea doesn't seem to have become popular with implementers." And anyway, this is an rvalue. stroustrup.com/bs_faq2.html#delete-zero
    – Qwertie
    Dec 21, 2018 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.