0

This is regarding deletion of this pointer for an object allocated on heap.Program is as follows,

class Sample{
      private:
              int m_value1;
              int m_value2;

      public:
             Sample();
             void setValues(int m_value1, int m_value2);
             void display();
      };

Sample::Sample(){
                 this->m_value1 = 0;
                 this->m_value2 = 0;
                 }

void Sample::setValues(int m_value1, int m_value2){
     this->m_value1= m_value1;
     this->m_value2 = m_value2;
     display();
     if(this != NULL)
     {
             cout <<"Deleting this pointer!"<<endl;
             delete this;
             cout <<"this->m_value1 is "<<this->m_value1<<endl;
             cout <<"this->m_value2 is "<<this->m_value2<<endl;

     }
     }

void Sample::display(){
     cout <<"Values are "<<this->m_value1<<" and "<<this->m_value2<<endl;

     }

int main(){
    Sample *obj1 = new Sample();;
    obj1->setValues(4,6);
    obj1->display();
    getch();
}

OUTPUT:

Values are 4 and 6
Deleting this pointer!
this->m_value1 is 65535
this->m_value2 is 6
Values are 65535 and 6

Why does the value for m_value1 disappear whereas it stays for the other variable m_value2?

taskinoor
  • 45,586
  • 12
  • 116
  • 142
Rajeev Mehta
  • 649
  • 9
  • 18
  • [related](http://stackoverflow.com/a/7039749/220636) – nabulke Feb 13 '12 at 11:31
  • maybe you have to study the `this` meaning. – vulkanino Feb 13 '12 at 11:32
  • 1
    First of all, `this` should _never_ be `NULL` when inside a non-static member function. Second of all, you should **never** delete `this`! What you are doing is like tearing down a bridge while still standing on it. – Some programmer dude Feb 13 '12 at 11:33
  • 2
    @JoachimPileborg There's nothing wrong with `delete this`; it's actually a fairly common idiom. Of course, the usual rules apply: once you've deleted the object, any access to it is undefined behavior. Whether the access is through `this` or some other pointer doesn't change anything. – James Kanze Feb 13 '12 at 11:49

4 Answers4

6

Accessing member variables after delete this is undefined behavior - anything can happen, including unexpected data being read, program crashing or anything else. The object has been destroyed and deallocated and you try to dereference an invalid pointer. Once delete this has happened - don't try to access the member variables, don't try t call member functions and don't do anything else (including comparing or casting) to this pointer.

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979
2

Why does the value for m_value1 disappear whereas it stays for the other variable m_value2?

You are accessing memory after you deleted it. That means you are invoking undefined behavior. So anything might happen, and your observation is as right or wrong as anything else.

What's likely going on is that the part of memory where m_value1 was stored is reused for something else, whilst the storage for m_value2 has not been used for something else (yet).

nos
  • 223,662
  • 58
  • 417
  • 506
1

It's a matter of chance, probably the operator<< or whatever your do after deleting this allocates enough memory to overwrite the one and not the other.

But when you do something that shouldn't be done you should not rely on any particular consequences.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
0

Dangerous and unpredictable outputs expected including system crash! Do not delete location pointed to by this directly. its not a good programming practice. and don't use memory after deleting. and even if u have to put it inside an assert()

this != NULL // this will never be NULL if used properly.
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112