0

Suppose class A has a regular member function and a virtual member function, and I have two pointers pointing to the same A object. Suppose one ptr deleted the object, while the other pointer is not aware of that. What will happen if the other pointer tries to call the regular member function and the virtual member function?

I understand it is not recommended behavior of utilizing a pointer after the object is deleted, and would emphasize on the difference between regular function versus virtual function, even static member function. Exactly how does compiler treats regular function, virtual function, and even static function at compile time?

Thanks!

Sarah
  • 453
  • 7
  • 16
  • 2
    Pretty sure that's UB – Alejandro Jul 08 '15 at 14:54
  • Already answered at http://stackoverflow.com/a/11887733/981959 and the excellent "hotel room" answer it links to. – Jonathan Wakely Jul 08 '15 at 15:08
  • Thank you for pointing to the "hotel room" answer. My question is actually more emphasizing on the difference between regular function versus virtual function. I understand it is not recommended behavior of utilizing a pointer after the object is deleted. But exactly how does compiler treats regular function, virtual function, and even static function at compile time? That is the question that I'm more interested into. Maybe I didn't explain it well enough in the previous post. For this aspect, can you recommend an answer? Thanks! – Sarah Jul 08 '15 at 15:32

2 Answers2

3

If you have two pointers to the same object, and you delete one, then the other is left with a dangling pointer. Trying to access anything after that is undefined behavior.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
2

most compilers implement member functions as 'ordinary' functions with additional pointer parameter called 'this'.

So as long as you are not using anything pointed by this - such member function can work (yet such function probably shouldn't be a member).

Virtual functions are implemented differently - vtable has to be accessed via this prior to calling function (to determine which one to call).

Please remember that this difference is implementation specific - calling any method on deleted object is undefined behavior and should be avoided.

Hcorg
  • 11,598
  • 3
  • 31
  • 36
  • Thank you for the reply. This is what I was looking for! Follow up on your answer, if the ordinary function didn't use this pointer, but utilize the private member variables, how is its behavior then? – Sarah Jul 08 '15 at 15:33
  • if the deletion is performed in one thread and utilized in another thread, deletion is done prior to the accessing, should I expect the same behavior? – Sarah Jul 08 '15 at 15:40
  • @Sarah: if you manage objects from one thread, and modify them (or their pointers etc.) from another, you should first of all expect troubles :) But yes - it does not matter who or when deletes object - it's dangling pointer behavior will be the same - undefined. – Hcorg Jul 08 '15 at 16:20
  • Thank you for the reply. – Sarah Jul 08 '15 at 17:02