ReventonMC
ReventonMC

Reputation: 15

C++ after delete

I am new to C++ and I was wondering why...

#include <iostream>
using namespace std;
class myClass{
    public:
        void myMethod(){
            cout << "It works!" << endl;
        }
        myClass(){
            cout << "myClass is constructed!" << endl;
        }
        ~myClass(){
            cout << "This class is destructed!" << endl;
        }
};
int main()
{
    myClass c;
    c.myMethod();
    myClass *e = &c;
    delete e;
    cout << "This is from main" << endl;
    return 0;
}

So up there is the code. and the output is

myClass is constructed!
It works!
This class is destructed!

I am wondering where did the "This is from main" output go away.. does C++ doesn't execute codes after delete keyword?

Upvotes: 0

Views: 191

Answers (4)

Makketronix
Makketronix

Reputation: 1460

Perhaps it is good idea for you to read about stack and heap memory.

You must free only when you malloc (or other variations such as calloc).

For example:

char *c = (char *)malloc(255);
...
free(c)

You must delete only if you use new.

MyClass *e = new MyClass()
... 
delete e

You must delete[] only if you use new[]

char *data = new char[20]
...
delete[] data

Now, if you do something like this:

...
{
  int x;
  x = 3;
}

x will be destroyed after the bracers because it is out of scope. This puts x on the stack. However, if you use malloc, new, or delete, the variable itself can be lost if you are not careful, but the memory is allocated. This is memory leak.

What you have is even more dangerous. You are deleting something which was not allocated. The behavior is not defined. With time and patience, a skilled hacker can study the behavior and may be able to break into your system and acquire same privileges as your program.

Upvotes: 0

krzaq
krzaq

Reputation: 16421

You can only delete objects that have been created with new.

What you're doing is UB, by the means of double deletion.

By the way, while in this case your program stopped execution right after the statement that had UB, it doesn't necessarily have to happen this way because

However, if any such execution contains an undefined operation, this International Standard places no requirement on the implementation executing that program with that input (not even with regard to operations preceding the first undefined operation).

Upvotes: 9

NathanOliver
NathanOliver

Reputation: 180660

You have undefined behavior. You are not allowed to delete something that was not allocated with new. In doing so you have undefined behavior and your program is allowed to do what it wants.

Most likely you should have received some sort of hard fault that stopped the program from running.

Upvotes: 3

Edward Strange
Edward Strange

Reputation: 40867

You caused undefined behavior by deleting something that was not newed. You don't need to delete stuff that just points at some general location. You only delete that which you created by calling new (not placement new!).

Upvotes: 0

Related Questions