1

Base* optr=new Derived();

delete optr;

I know that if Base class has a non-virtual destructor, only ~Base() destructor is going to be called when deleting optr pointer. But I found out that even without ~Derived() destructor being called the memory that was taken by the Derived Object was freed. So my question is can an object get freed without calling it's destructor?

If the answer is yes, can I use a non-virtual destructor if my Derived class does not contain any dynamically allocated variables so I don't care if it did not get called?

j0k
  • 22,600
  • 28
  • 79
  • 90
AlexDan
  • 3,203
  • 7
  • 30
  • 46

5 Answers5

4

From the viewpoint of standard C++, the answer is simple: the result is undefined behavior, so what you get is completely unpredictable

I'm a little puzzled why you'd care anyway. If you can eliminate all the virtual functions from a class, each instance becomes smaller (by the size of a vtable pointer). Using such a thing as a base class rarely makes sense though -- for use as a base class to be sensible, you pretty much need to have at least one virtual function in the base for the derived class to override. Once you have a virtual function (any virtual function) adding more is essentially free -- objects don't grow any larger by adding more virtual functions.

To answer your question directly: yes, the memory can be freed without the destructor being invoked. The real question is what will happen when you make that happen (and there's really no answer to that question).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
2

The thing about undefined behaviour is that sometimes it seems to work.

The standard doesn't say that your program must fail if the base class destructor is not virtual, it says that it must work when the destructor is virtual.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
1

You might be able to get away without a virtual destructor if the derived class doesn't add any members. The memory footprints will be the same, and the members will all be destroyed in the base class destructor. However this is not guaranteed by the standard and you'll be at the mercy of your compiler's implementation.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • In the list of *mights*, there are other cases, like the derived type must not have `operator delete` defined... (i.e. if it has one, then it won't be called unless the destructor is virtual). I once saw in the news that a kid accidentally shot himself through the head with a harpoon and he did not take any damage at all, so you *might* get away from shooting yourself with a harpoon, but I would not recommend doing it... – David Rodríguez - dribeas Apr 16 '12 at 16:58
  • @David, I don't play with harpoons so I'm safe! Seriously I've never used `operator delete` and I've never seen anyone else use it either. – Mark Ransom Apr 16 '12 at 17:07
1

There're two distinct things: calling object destructor, and freeing the memory.

Standard heap specification does not require you to pass the memory block size when you free it. That is, the heap implementation should deduce the memory block size itself.

Hence - yes. If you Derived does not contain extra things that must be destroyed by appropriate means (such as memory allocated on heap, file handles and etc.) - you don't need the virtual destructor.

valdo
  • 12,632
  • 2
  • 37
  • 67
0

Look over here : Virtual destructor

Erwald
  • 2,118
  • 2
  • 14
  • 20