"delete this" in C++?


Delete is an operator that is used to Deallocate storage space of Variable.

This pointer is a kind of pointer that can be accessed but only inside nonstatic member function and it points to the address of the object which has called the member function.

This pointer holds the address of the current object, in simple words, you can say that this pointer points to the current object of the class

Whenever we call a member function through its object, compiler secretly passes the address of calling that object as the first parameter in member function as this pointer.

Generally, delete operator should not be used for this pointer. Suppose, if used, then the following points must be considered.

Ideally, delete operator should not be used for this pointer. However, if used, then the following points must be considered.

  • delete operator works only for objects allocated using operator new (See this post). If the object is created using new, then we can do delete this, otherwise, the behavior is undefined.

filter_none
edit
play_arrow
brightness_4
class A {
   public:
   void fun() {
      delete this;
   }
};
int main() {
   /* Following is Valid */
   A *ptr = new A;
   ptr->fun();
   ptr = NULL; // make ptr NULL to make sure that things are not accessed using ptr.
   /* And following is Invalid: Undefined Behavior */
   A a;
   a.fun();
   getchar();
   return 0;
}
  • Once delete this is done, any member of the deleted object should not be accessed after deletion.

filter_none
edit
play_arrow
brightness_4
#include<iostream>
using namespace std;
class A {
   int x;
   public:
   A() { x = 0;}
   void fun() {
      delete this;
      /* Invalid: Undefined Behavior */
      cout<<x;
   }
};

The best thing is to not do delete this at all.

Deleting this pointer inside member function is wrong, we should never do that. But if we do these following things can happen,

  • If that object from which this member function is called is created on the stack then deleting this pointer either crash your application or will result in undefined behavior.

  • If that object from which this member function is called is created on the heap using new operator, then deleting this pointer will destroy the object. It will not crash the application at that particular time but after it, if some member function will try to access the member variable through this object then the application will crash.

Example

#include <iostream>
class Dummy {
   int m_value;
   public:
   Dummy(int val) :
   m_value(val)
   {}
   void destroy();
   void displayValue();
   void displayText();
};
void Dummy::destroy() {
   delete this;
}
void Dummy::displayValue() {
   std::cout << this->m_value << std::endl;
}
void Dummy::displayText() {
   std::cout << "Not accessing any member function" << std::endl;
}
int main() {
   Dummy * dummyPtr = new Dummy(5);
   dummyPtr->destroy();
   dummyPtr->displayText();
   return 0;
}

Once we deleted this pointer in destroy() member function, after this calling displayText() is safe because it is not accessing any member function. But calling displayValue() will crash the application because it is accessing member variable through a dangling pointer i.e. deleted this pointer.

Updated on: 20-Aug-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements