Deleting this pointer and internal details of this pointer

Deleting this pointer and internal details of this pointer

In this article we will get to know about this pointer and what happens if we delete this pointer inside member function.

What is this pointer?

The pointer that can be accessed only inside non static member functions which points to the address of the object calling the member function is called this pointer. when the object is called  through a member function then the compiler passes the address of the object calling as the first parameter in the member function as this pointer. This allows us to access the data  members inside the member function.

This pointer is essential to know how the objects access the functions and data members of a class .  Each pointer gets its own copy of the data member and all of them access the same function definition.

When a non-static class member is used in any of the contexts where the ‘this’  keyword is allowed which helps in member access expression. The ‘this’ pointer is passed as a hidden argument to all non-static member functions to all the non-static member functions calls and is available as a local variable within the body of all non-static functions .

 Deleting this pointer inside member function ?

But Deleting this pointer inside member function is wrong ,it’s actually never done but certain things can be done to fix i.e.

  1. Deleting this pointer either results in crashing the application or undefined behavior if the object from which this member function is called is created on stack.
  2. Deleting this pointer results in destroying the object if that object from which this member function is called is created on heap using new operator.

And once this pointer is deleted in the destroy() member function ,after this calling display text() is more safe because it’s not accessing any member  function .

Whereas, it is possible to execute delete this  if only the program can guarantee that the object was allocated by new, however this renders that every pointer to the deallocated object invalid including this pointer itself .

#include <iostream>
class Example
{
    int valueM;

public:
    Example(int value) : valueM(value)
    {
    }
    void deletePtr();
    void showValue();
    void showText();
};
void Example::deletePtr()
{
    delete this;
}
void Example::showValue()
{
    std::cout << this->valueM << std::endl;
}
void Example::showText()
{
    std::cout << "Not acessing any of the member function" << std::endl;
}
int main()
{
    Example *example = new Example(5);
    example->deletePtr();
    example->showText();
    //Calling the showValue( ) function will crash the  system
    // because the pointer has already been deleted.
    //example->showValue();
    /*
   * Creating a object and calling the "this pointer"
   * inside the member function will also crash the program
      Example obj;
      obj.deletePtr()
   */
    return 0;
}
Output :
Not acessing any of the member function

Once we deleted the this pointer in deletePtr() member function, after this calling showText() is safe because it is not accessing any member function. But calling showValue() will crash the application because it is accessing member variables through a dangling pointer i.e. deleted this pointer.