-2

In the following program, how is p getting deleting twice, as it is pointing to same memory location?

#include <iostream>

using namespace std;

class Test {
public:
    int *p;

    Test() {
        cout << "Constructor is executed\n";
    }

    ~Test() {
        cout << "Destructor is executed\n";
        delete[] p;
        cout << "p deleted\n";
    }

    void make() {
        p = new int[5];
    }

    //here destructor is called expllicitly
    void show() {
        this->Test::~Test();
    }
};

int main() {
    Test t;
    t.make();
    t.show();
    return 0;
}

Output:

Constructor is executed
Destructor is executed
p deleted
Destructor is executed
p deleted

chris
  • 60,560
  • 13
  • 143
  • 205
Vikas
  • 432
  • 5
  • 18
  • 2
    I really don't understand the question. The destructor deletes `p`, and it's being called twice. Therefore `p` is being deleted twice. What's surprising in that? – Jon Jul 03 '14 at 20:01
  • Please don't put html formatting inside your code block. And some kind of reasonable indentation system would make your code a lot more readable. – JBentley Jul 03 '14 at 20:02
  • Does this compile - the braces are all over the shop – Ed Heal Jul 03 '14 at 20:03
  • Please please please read the formatting tips that come up as you create the question. Preferably, read [Formatting Help](http://stackoverflow.com/editing-help). – chris Jul 03 '14 at 20:03
  • @Jon I believe the OP is asking how it's possible to delete an already deleted object, not why the statement `delete[] p` is executed twice. – JBentley Jul 03 '14 at 20:07
  • @Jon I am not able to understand one thing that we have assigned memory to me once how its possible to delete already deleted pointer – Vikas Jul 03 '14 at 20:08
  • Possible duplicate: [What happens when you deallocate a pointer twice or more in C++?](http://stackoverflow.com/q/2746677/1227469) – JBentley Jul 03 '14 at 20:10
  • @user3742158 That's like asking *why can I call the same function more than once?* You're executing the statement `delete[] p;` twice, hence you delete the allocated memory twice, and second call is undefined behavior. – Praetorian Jul 03 '14 at 20:11
  • @Praetorian I'm not sure that's a good analogy. A function can be defined such that the behaviour differs depending on whether it makes sense to call it or not. Perhaps the OP expects the second call to throw an exception, or something similar. – JBentley Jul 03 '14 at 20:13
  • @JBentley You *might* get different behavior in the second `delete` call too :) Anyway, the linked dupe answers the question. – Praetorian Jul 03 '14 at 20:14
  • @JBentley exactly I was expecting an error or crash or something but it just worked fine – Vikas Jul 03 '14 at 20:16
  • @Vikas Read the linked duplicate; it doesn't *work fine*, it's undefined behavior. There's no telling what'll happen if you deallocate the same memory twice. – Praetorian Jul 03 '14 at 20:17
  • @Vikas That's why we say the behaviour is "undefined". What happens depends on the implementation. Perhaps the heap gets corrupted due to the second call, but by the time your program terminates, it hasn't made any difference to the rest of your code. Or, perhaps your specific implementation has made sure nothing bad will happen. You can't however rely on your program to work correctly 100% of the time when you have UB, because the standard does not require a compiler to produce correctly behaving code when you do something like that. I suggest you read the duplicate link and all the answers. – JBentley Jul 03 '14 at 20:18
  • @JBentley thanks for you advice i have made some modification in the code and YES its printing garbage values / Undefined behave. – Vikas Jul 03 '14 at 20:22

2 Answers2

1

You are calling the destuctor once yourself (Case 1) then the destructor is getting called on the object when it is no longer in scope (Case 2).

1) Show() calls the destructor

t.show();

2) t falls out of scope at the end of main, therefore calling the destructor.

int main()  
{
    Test t;
    t.make();
    t.show();
    return 0;
}  // t will now call its destructor
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • I don't think this is really what the OP is asking. He's not asking why the destructor is called twice, he's asking how `p` can get deleted a second time when it has already been deleted. – JBentley Jul 03 '14 at 20:05
0
/*
  p is deleted twice becasue the destructor, Test::~Test() is called twice.
  it is called once explicitly in show() , and once implicitly when main() returns.
 */
#include <iostream>

using namespace std;

class Test
{
public:
    int *p;
    Test() { cout << "Constructor is executed\n"; }
    ~Test()
    { 
        cout << "Destructor is executed\n";
        delete []p;
        cout<<"p deleted\n";
    }
    void make(){ p = new int[5];}
    /**here destructor is called expllicitly**/
        void show() { this->Test::~Test(); }
};

int main()  
{
    // Right here you create an object of type Test, which is destroyed when the function returns.
    Test t; 
    t.make();
    t.show();
    return 0; // Right here is the second implicit call of the destructor, Test::~Test() .
}
llewelly
  • 339
  • 1
  • 4