0
#include<bits/stdc++.h>
using namespace std;
class A {

 public :
     ~A(){
    cout << " A is destroyed " << endl;
    }
};

class B : public A
{

 public :
     ~B(){
         cout << " B is destroyed " << endl;
    }
};
int main()
{
    B obj;
    B * p = &obj;
    delete p;
    return 0;
}

In the main function I am creating only one object of class B, which inherits class A. When I am deleting that object using the pointer , destructor is called and prints message But then , I am not able to understand that why destructor is called twice ?

Neer Patel
  • 399
  • 3
  • 11
  • You should post the output of the program. – BiagioF Aug 19 '16 at 20:54
  • 5
    @BiagioFesta: That would be meaningless in this case. Besides, how do you post those nasal demons? – MSalters Aug 19 '16 at 20:57
  • @BiagioFesta ... I am sorry , but I am not able to paste my output here . I am new user , and don't know how to do it . sorry :| – Neer Patel Aug 19 '16 at 20:58
  • 1
    You probably should define a [virtual destructor](http://stackoverflow.com/q/461203/10077) in `A` as well, although I don't believe that has anything to do with your immediate problem. – Fred Larson Aug 19 '16 at 21:00
  • 2
    This is ill-formed code: it's a very bad idea to try to `delete` a pointer pointing to a local variable. – Daniel Schepler Aug 19 '16 at 21:00
  • side note : dont include std bitsc++ – Hatted Rooster Aug 19 '16 at 21:00
  • Your code has undefined behaviour, that's why. – Kerrek SB Aug 19 '16 at 21:01
  • @MSalters Just my local time, I'm very tired and I didn't look properly the code. Sorry, I'd edited just two second after the *enter* of the comment :) – BiagioF Aug 19 '16 at 21:01
  • @GillBates: I think that header is deliberately provided by GCC as a convenience extension to get the entire standard library. It's not portable, but it's certainly working as intended. – Kerrek SB Aug 19 '16 at 21:02
  • 2
    The right tool to solve such problems is to use your debugger, but not to ask at Stack Overflow before you did so. Tell us all your observations you made when inspecting your code stepping through line by line in 1st place. Also you might want to read [**How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)**] At least leave us with a **[MCVE]** that reproduces your problem. (This is a personal stock comment provided by πάντα ῥεῖ™) – πάντα ῥεῖ Aug 19 '16 at 21:07
  • Don't `delete` it if you didn't `new` it. – Pete Becker Aug 19 '16 at 21:42

3 Answers3

11

Because you have a variable on the stack, so the destructor is automatically called at the end of the scope.

B obj; // <- Constructor called.
B * p = &obj;
delete p; // <- Bad, undefined behaviour, but destructor called. 
return 0; // <- Destructor called as `obj` goes out of scope. 

You have caused undefined behaviour with this line:

delete p;

Remember that you should only ever delete memory that you have explicitly created (ie. with new).

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • Do you mean that when I am deleting an object with "delete" keyword, it still has space in memory ? – Neer Patel Aug 19 '16 at 21:05
  • 3
    @NeerPatel No i mean that doing that causes undefined behavior. You should never `delete` what you didn't `new`. – Fantastic Mr Fox Aug 19 '16 at 21:06
  • okay . Got it. I have almost understood what you are trying say. But still want your opinion. Does that mean , all the objects that are allocated in **Heap** section(dynamically allocated) can be removed using "delete" keyword . But objects that are stored in **stack** section will not be removed by using "delete" keyword ? – Neer Patel Aug 19 '16 at 21:13
  • @NeerPatel Objects initialized by using the `new` keyword can be destroyed using the `delete` keyword, this includes placement new's etc. trying to remove something that was not `new`ed with the `delete` keyword will cause undefined behavior, it might crash (segfault), it might work, it might even steal your car. *Undefined behavior* is undefined .... – Fantastic Mr Fox Aug 19 '16 at 21:16
  • very helpful . Thank You – Neer Patel Aug 19 '16 at 21:17
  • 1
    @NeerPatel No problem, remember to mark as correct if this answered your question. – Fantastic Mr Fox Aug 19 '16 at 21:38
3

First time destructor is called when line delete p; is executed. Second time - when B obj; goes out of scope of your main

mvidelgauz
  • 2,176
  • 1
  • 16
  • 23
3

If you create an object in the stack like you did in this case you should not call delete on it. the object will be automatically destroyed at the end of the scope (the enclosing}). To answer you question calling delete resulted in calling the destructor twice

Kadhem Ouerghi
  • 103
  • 1
  • 5