Update appNew update is available. Click here to update.

Delete this in C++

Aditi
Last Updated: Jan 31, 2023
EASY

Introduction

The Delete operator is used to deallocate Variable storage space. This pointer is a particular type of pointer that may only be accessed within non-static member functions and refers to the object's address that invoked the function. This pointer contains the address of the current object; in other words, this pointer points to the class's current object. In this article, we will learn about the delete this concept in detail. Let's dive into the article to get more information about this concept.

Delete This

When calling a member function through its object, the compiler covertly transmits the address of calling that object as the first parameter in the member function. This pointer should not, in general, be deleted with the delete operator. 

Assume that if it is used, the following things must be considered. ‘this’ pointer should not, in most cases, be deleted with the delete operator. 

  • The delete operator is only applicable to objects created with the new operator. If new was used to create the object, we could remove it; otherwise, the action is unknown.

 

Program:

class temp {
   public:
   void func() {
      delete this;
   }
};
int main() {
   /* Following is Valid */
   temp *p = new temp;
   p->func();
   p = NULL;
   /* And following is Invalid: Undefined Behavior */
   temp x;
   x.func();
   getchar();
   return 0;
}

 

Output:

free(): invalid pointer

 

  • After this has been completed, no members of the removed object should be accessible.

 

Program:

#include<iostream>
using namespace std;
class temp {
   int num;
   public:
   temp() { num = 0;}
   void func() {
      delete this;
      /* Invalid: Undefined Behavior */
      cout<<num;
   }
};

int main()
{
  temp* obj = new temp;
  obj->func();
  return 0;
}

 

Output:

0

Delete this with template class

It's never a good idea to delete this reference inside a member function. However, if we do so, the following things may occur:

  • If the object that calls this member function is constructed on the stack, erasing this reference may crash your program or cause undefined behavior.
  • If this member function is called from an object created on the heap with the new operator, removing this pointer will destroy the object. It will not crash the program at that time, but the application will crash later if any member function tries to access the member variable using this object.

Program:

#include <iostream>
using namespace std;
class temp {
   int value;
   public:
   temp(int val) :
   value(val)
   {}
   void destroy() {
        delete this;
   }
   void displayValue() {
        std::cout << this->value << std::endl;
    }
   void displayText()  {
        std::cout << "Not accessing any member function" << std::endl;
    }
};

int main() {
   temp * dummyPtr = new temp(5);
   dummyPtr->destroy();
   dummyPtr->displayText();
   return 0;
}

Output:

Not accessing any member function

 

Following the deletion of this pointer in the destroy() member function, invoking displayText() is safe since it does not contact any member functions. However, invoking displayValue() will cause the application to crash since it is accessing a member variable through a dangling reference, which has been removed.

Try and compile with online c++ compiler.

Frequently Asked Questions

What is the delete operator in C++?

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

What is this pointer?

All member functions take this pointer as an implicit argument. As a result, this may refer to the calling object within a member function. Friends are not members of a class; hence there is no this pointer in friend functions. This pointer is only available to member functions.

What is the class in C++?

In C++, a class is a user-defined type or data structure that includes data and functions as members and whose access is controlled by the three access specifiers private, protected, and public.

Conclusion

In this article, we have extensively discussed the delete this concept of the C++ programming language.

We hope this blog has helped you enhance your knowledge regarding this pointer and delete operator. Some official documentation on the C++ programming language that can help you improve your understanding is C++ documentation.

If you would like to learn more, check out our articles on pointerslearn pointers, and pointers in C++

Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. Happy Coding!

Was this article helpful ?
1 upvote