0
int main()
{
    myClass obj;

    ....    /* doing things */
    ....

    delete &obj; /* illegal */
}

Lets assume myClass is a proper C++ class with everything in place. Now, I know this is illegal and this program will crash in runtime. Fist thing is that code is trying to delete a stack object and then again after the scope finishes it will be destoryed once again. I want to know the internals of this illegal operation. i.e. what will happen with delete, will it call the destructor? It may look like crazy to you but please help me to understand.

paper.plane
  • 1,201
  • 10
  • 17
  • 4
    It will *probably* crash, but as is the case with undefined behavior it might, on the other hand, not crash, or crash most of the time but not always, or even cause [nasal demons](http://www.catb.org/jargon/html/N/nasal-demons.html). More to the point of your question, there is really no way to tell how this will be handled, the nature of undefined behavior is most of the time unpredictable. – Some programmer dude Aug 02 '13 at 12:42
  • 1
    It's not hard to find out what it actually does by inspecting generated code in non-optimized builds – sehe Aug 02 '13 at 12:42
  • 1
    It's not a dupe. OP here isn't asking whether it's safe, they know it isn't. – jrok Aug 02 '13 at 12:43
  • 1
    @jrok the answer of that thread still 100% applies to this question: it's undefined behavior. – nikolas Aug 02 '13 at 12:44

4 Answers4

6

The behavior is undefined. What happens will depend on the details of the class, what else is going on, how the program's memory manager is implemented, what compiler you used, what system you're running on, and probably a host of other things that I haven't taken the time to think about. This is pretty much like asking for the details of what happens when a chemical storage tank explodes.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
2

You are missing one important thing - aside from calling destructor, delete will free memory allocated with new and it will return it to the heap. Calling free on stack allocated variable is not safe. To see if it will call destructor first, just put some cout inside myClass destructor - on my configuration it calls destructor before segmentation fault.

#include <iostream>

using namespace std;

class myClass
{
 public:
     ~myClass(){ cout << "Destructor" << endl; }

};


int main()
{
        myClass A;
        delete &A;
        cout << "End of main\n";
        return 0;
}

From MSDN:

When delete is used to deallocate memory for a C++ class object, the object's destructor is called before the object's memory is deallocated (if the object has a destructor).

Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91
1

This is undefined behaviour. The standard, the words by which we all live and look up to, says:

Otherwise, the value supplied to operator delete(void*) in the standard library shall be one of the values returned by a previous invocation of either operator new(size_t) or operator new(size_t, const std::nothrow_t&) in the standard library

So it's undefined behaviour, anything can happen, and it will depend on the implementation of the object being wrongly deleted, the compiler, luck and other things. Asking "what happens" internally does not make sense because anything can happen. Most probably, since the delete operator assumes you're using it correctly, you end up introducing some unpredictable changes in the stack, which can mess anything up. Maybe your previous frame pointer gets overwritten, maybe some ridiculous return value gets pushed to a function, maybe the saved registers get overwritten so an attempt to restore register state results in a very illegal situation. Whatever happens, it's ugly.

DUman
  • 2,560
  • 13
  • 16
0

Rule of thumb:

For each new there should be exactly one delete.
For each new[] there should be exactly one delete[].
For each malloc or calloc there should be exactly one free.
For each stack allocated object there shouldn't be any explicit deletion.

Anything else will cause undefined behaviour.


i.e. what will happen with delete, will it call the destructor?

Yes, delete will call the objects destructor first.

gifnoc-gkp
  • 1,506
  • 1
  • 8
  • 17