0

I am aware of pointer and memory allocation but I am not getting the below program. I am getting wrong output.

int main()
 {
    int *old = new int(10);
    cout<<*old<<" "<<old<<endl;  //10 0x1b3ac20(Address)
    delete old;
    cout<<*old<<" "<<old<<endl;  //0 0x1b3ac20(Address)
    *old = 20;
    cout<<*old<<" "<<old<<endl;  //20 0x1b3ac20(Address)

    int *latest = new int(12);
    cout<<*latest<<" "<<latest<<endl; //12 0x1b3ac20(Address)  
    delete latest;
    cout<<*latest<<" "<<latest<<endl; //20 0x1b3ac20(Address). why 20?
    return 0;
 }

I don't understand the below statements.

delete old;
*old = 20;--------------------------->How can we assign value if memory deleted
cout<<*old<<" "<<old<<endl;-------(1)

delete latest;--------------------------------(2)
cout<<*latest<<" "<<latest<<endl;------------->If I delete the latest then why *latest still printing 20

0 Answers0