deleting this pointer and internal details of this pointer

In this article we will discuss what is exactly this pointer and what happens if we delete this pointer.

what is this pointer?

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

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

So, we can access calling objects member variables inside member function

Suppose we have a class Dummy and it has a member variable m_value and a member function displayValue().

 

[showads ad=inside_post]

 

So, whenever we call the displayValue() member function from Dummy’s object, compiler secretly passes the address of Dummy’s object as first parameter to member function i.e

Dummy * ptr = new Dummy(5);
ptr->displayValue();

is secretly converted in to following by compiler,

Dummy * ptr = new Dummy(5);
ptr->displayValue(ptr);

also definition of displayValue is,

void Dummy::displayValue()
{
  std::cout << m_value << std::endl;
}

is secretly converted in to following by compiler,

void Dummy::displayValue(Dummy * const this)
{
  std::cout << this->m_value << std::endl;
}

So, when we accessed m_value, it uses the calling object to access the member value.

This is how this pointer works.

But,

what if we delete this pointer inside member function ?

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

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

2.) If that object from which this member function is called is created on 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 application will crash.

Checkout this 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();

  //Calling this function can crash your application
  // because duppyPtr is already deleted.

  //dummyPtr->displayValue();

  /*
   * Creating an object on stack and calling deleting this pointer
   * inside member finction will also crash the application
      Dummy obj;
      obj.destroy()

   */

  return 0;
}

Once we deleted the 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top