3

As, the title says:

Why is calling non virtual member function on deleted pointer an undefined behavior?

Note the Question does not ask if it is an Undefined Behavior, it asks Why it is undefined behavior.


Consider the following program:

#include<iostream>
class Myclass
{
    //int i
    public:
      void doSomething()
      {
          std::cout<<"Inside doSomething";
          //i = 10;
      }
};

int main()
{
    Myclass *ptr = new Myclass;
    delete ptr;

    ptr->doSomething();

    return 0;
}

In the above code, the compiler does not actually dereference this while calling member function doSomething(). Note that the function is not an virtual function & the compilers convert the member function call to a usual function call by passing this as the first parameter to the function(As I understand this is implementation defined). They can do so because the compiler can exactly determine which function to call at compile time itself. So practically, calling the member function through deleted pointer does not dereference the this. The this is dereferenced only if any member is accessed inside the function body.(i.e: Uncommenting code in above example that accesses i)
If an member is not accessed within the function there is no purpose that the above code should actually invoke undefined behavior.

So why does the standard mandate that calling the non virtual member function through deleted pointer is an undefined behavior, when in fact it can reliably say that dereferencing the this should be the statement which should cause undefined behavior? Is it merely for sake of simplicity for users of the language that standard simply generalizes it or is there some deeper semantic involved in this mandate?

My feeling is that perhaps since it is implementation defined how compilers can invoke the member function may be that is the reason standard cannot enforce the actual point where UB occurs.

Can someone confirm?

3
  • 4
    The standard does not mandate anything; that is the whole idea of undefined behaviour. Saying the thing you claim it can "reliably say" would be mandating something. Commented Dec 23, 2012 at 16:23
  • 1
    The compiler can never "dereference" anything. Dereferencing is a part of the language structure. It has nothing to do with code generation. It's dangerous to confuse the language and the generated code. The language says that calling a member function evaluates the implicit instance argument, and that's it. Commented Dec 23, 2012 at 16:23
  • 3
    If you want the behaviour you're using, you should make the member function static. It's morally safe to call if and only if it doesn't need any per-object state, and that means it should be static. Commented Dec 23, 2012 at 16:24

4 Answers 4

9

Because the number of cases in which it might be reliable are so slim, and doing it is still an ineffably stupid idea. There's no benefit to defining the behaviour.

Sign up to request clarification or add additional context in comments.

5 Comments

+1. There is good reason to support this with static class members (and last I checked it still is) which have no 'this'. It has saved me a boatload of typing in the past. But for regular members (virtual or not) I see no benefit whatsoever.
Also +1 - if you have a non-static member function that doesn't do anything to the state of your object, why the hell is it a member function? :D
Although at first glance it would seem that there would be no reason for a method like this not to be static, I have come across a need to do this. For example, static methods cannot implement an interface, so something like a utility method whose implementation is hidden behind an interface (or there could be multiple implementations of the utility) is a pure method which must be an instance method in order to implement the interface. Also, you may want a utility method to be virtual so it could be overridden by another utility class.
Static methods can implement static interfaces. If the interface is non-static then you need a valid this to look up the intended function.
@jam40jeff, if the function is virtual then you need to "dereference this" to access the vtable, so it's not relevant to this question, which specifically says non-virtual.
6

So why does the standard mandate that calling the non virtual member function through deleted pointer is an undefined behavior, when in fact it can reliably say that dereferencing the this should be the statement which should cause undefined behavior?

[expr.ref] paragraph 2 says that a member function call such as ptr->doSomething() is equivalent to (*ptr).doSomething() so calling a non-static member function is a dereference. If the pointer is invalid that's undefined behaviour.

Whether the generated code actually needs to dereference the pointer for specific cases is not relevant, the abstract machine that the compiler models does do a dereference in principle.

Complicating the language to define exactly which cases would be allowed as long as they don't access any members would have almost zero benefit. In the case where you can't see the function definition you have no idea if calling it would be safe, because you can't know if the function uses this or not.

Just don't do it, there's no good reason to, and it's a Good Thing that the language forbids it.

Comments

5

In C++ language (according to C++03) the very attempt to use the value of an invalid pointer is causing undefined behavior already. There's no need to dereference it for the UB to happen. Just reading the pointer value is enough. The concept of "invalid value" that causes UB when you merely attempt to read that value actually extends to almost all scalar types, not just to pointers.

After delete the pointer is generally invalid in that specific sense, i.e. reading a pointer that supposedly points to something that has just been "deleted" leads to undefined behavior.

int *p = new int();
delete p;
int *p1 = p; // <- undefined behavior

Calling a member function through an invalid pointer is just a specific case of the above. The pointer is used as an argument for the implicit parameter this. Passing a pointer is an non-reference argument is an act of reading it, which is why the behavior is undefined in your example.

So, your question really boils down to why reading invalid pointer values causes undefined behavior.

Well, there could be many platform-specific reasons for that. For example, on some platforms the act of reading a pointer might lead to the pointer value being loaded into some dedicated address-specific register. If the pointer is invalid, the hardware/OS might detect it immediately and trigger a program fault. In fact, this is how our popular x86 platform works with regard to segment registers. The only reason we don't hear much about it is that the popular OSes stick to flat memory model that simply does not actively use segment registers.


C++11 actually states that dereferencing invalid pointer values causes undefined behavior, while all other uses of invalid pointer value cause implementation-defined behavior. It also notes that implementation-defined behavior in case of "copying an invalid pointer" might lead to "a system-generated runtime fault". So it might actually be possible to carefully maneuver one's way through the labyrinth of C++11 specification and successfully arrive at the conclusion that calling a non-virtual method through an invalid pointer should result in implementation-defined behavior mentioned above. By in any case the possibility of "a system-generated runtime fault" will always be there.

5 Comments

Hard to disagree with any of that. Still, the idea that delete might make a pointer so invalid that the hardware would segfault just looking at it seems like its almost on the flying lizards level. As far as I know, there are no implementations of C++ in which the delete operator physically disassembles the memory chips, although of course the standard does not preclude such an implementation. I'm not sure I'd want it on my desk, though. :)
C++11 doesn't say that, the change to implementation-defined was made by a DR in October 2012, so is only in the latest WP, not any standard. @rici, The DR explains why accessing the value of an invalid pointer could segfault.
N.B. your example code would be OK if the implementation of delete p sets p to nullptr, which is allowed.
I've downvoted this, because it implies that an implementation which allows reading invalid pointer values must allow calling a member function through an invalid pointer, which is not a correct reading of the standard. A member function call such as p->foo() is defined to be equivalent to (*p).foo() (see [expr.ref]/2) which is still a dereference of the pointer and therefore undefined behaviour if p is invalid.
@rici there are no implementations of C++ in which the delete operator physically disassembles the memory chips Actually standard allows the computer to do whatever it wants with released memory so this kind of implementation is allowed if computer designers find it suitable for their purpose. Also, compiler might generate dereferencing for the pointer if that's the way it is required by underlying machine code or if some specific optimizations need to be done for specific platform. It might be that some microcontrollers do exactly that.
2

Dereferencing of this in this case is effectively an implementation detail. I'm not saying that the this pointer is not defined by the standard, because it is, but from a semantically abstracted standpoint what is the purpose of allowing the use of objects that have been destroyed, just because there is a corner case in which in practice it will be "safe"? None. So it's not. No object exists, so you may not call a function on it.

1 Comment

So the code above works, because the memory is freed but not rewritten? We just have freed adress?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.