Linked Questions

Popular Questions

Can't delete a dynamic object in C++

Asked by At

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"

Related Questions