-5

I've created a dynamic object in C++ using pointer. I set the value for the dynamic object. Then I delete pointer, but when I try to print the value of dynamic object, it is stay the same.

I have tried to print the value of pointer which is the address of the dynamic object, it is stay the same before and after I delete pointer.

#include<iostream>
using namespace std;

struct students
{
string name;
int agee;
};

int main()
{
students *p = NULL;
p = new students;
(*p).name = "Vu Trung Nghia";
(*p).agee = 20;
cout << p->name << " " << p->agee << endl;
delete p;
if(p == NULL)
    cout << "It was deleted";
else 
    cout << (*p).name << " " << (*p).agee << endl;
}

I expect the result is: p == NULL or can't print "Vu Trung Nghia 20" Actual result: "Vu Trung Nghia 20"

  • 2
    Are you getting a segmentation fault? – GKE Feb 03 '19 at 03:33
  • C++ programs are lazy. They do the absolute minimum required to get the job done. On the upside, that's what makes them fast. On the downside, they leave a lot of leeway for you to get shot in the foot. Do not assume anything that you did not ask for unless the C++ Standard explicitly states it happens. If you are not sure, look it up. If you're still not sure, assume you have to do it yourself. – user4581301 Feb 03 '19 at 03:46

2 Answers2

2

I expect the result is: p == NULL

There is no reason to expect that, since you never assigned p = NULL.

I have tried to print the value of pointer which is the address of the dynamic object, it is stay the same before and after I delete pointer.

This is the expected behaviour. Deleting a pointer has no effect on the value of the pointer.

but when I try to print the value of dynamic object, it is stay the same.

Behaviour of accessing a destroyed object is undefined. You don't ever want your program to have undefined behaviour.

When the behaviour is undefined, anything could happen. "I try to print the value of dynamic object, it is stay the same." is one of the possible behaviours.

So what deleting a pointer really is?

Assuming the pointer points to an object that was allocated with new, then delete destroys the pointed object, and deallocates the memory. Deallocation means that a subsequent new may re-use that memory.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • So what deleting a pointer really is? – Nghĩa Vũ Trung Feb 03 '19 at 03:39
  • @NghĩaVũTrung see the edit. – eerorika Feb 03 '19 at 03:41
  • @NghĩaVũTrung Regarding *subsequent new may re-use that memory.* This might not happen any time soon, leaving what you left in that memory both accessible and the same, fooling you into thinking the program works when it's just waiting for an opportune moment to make your life much more difficult. – user4581301 Feb 03 '19 at 03:44
0

delete frees the pointer from memory, it does NOT change its value or address, the value stays there, the calls which you do after deleteing it invokes Segmentation fault (which can easily be translated to your program having Undefined behvaior)...

This is the reason why people set the pointer to NULL after invoking delete...

delete p;
p = nullptr; // Stop memory leaking...

You will also find that you are no more able to modify the pointer:-

(*p).name = "Hello!"; /* Value is freed/destroyed from memory, cannot be 
                         modified anymore... */

Or just overload your own delete operator:

struct students
{
    string name;
    int agee;
    void students::operator delete  ( void* ptr ) {
        std::free(ptr);
        ptr = nullptr; // Stop memory leaking...
    }
};
Ruks
  • 3,886
  • 1
  • 10
  • 22
  • Terminology: `p = nullptr; // Stop memory leaking...` the leak has already been stopped. This (usually) makes using the pointer after deleting it immediately fatal. In `void students::operator delete ( void* ptr )` `ptr` has been passed by value. Changing where it points will have no effect on the caller. – user4581301 Feb 03 '19 at 06:43