When we perform a delete operation on a pointer, we actually are deleting the value pointed by the pointer and not the pointer itself(?). So after deletion, the pointer variable is still there somewhere in the memory having a valid address. I am just curious how this addresses are destroyed? Does this address, at some point in time, gets re-assigned to a newly declared pointer variable or does it just stay there in the memory? I googled about it but couldn't find any information :(
$ cat dangling_ptr.cpp
#include<iostream>
usingnamespace std;
int main()
{
int i=10;
int* p;
int* q = newint;
cout << "value pointed by q is\n"<<*q<<endl;
cout << "value stored in q is\n"<<q<<endl;
cout << "address of q is\n"<<&q<<endl;
p=q;
cout << "value pointed by p is\n"<<*p<<endl;
cout << "value stored in p is\n"<<p<<endl;
cout << "address of p is\n"<<&p<<endl;
delete q;
cout << "address of p is\n"<<&p<<endl;
cout << "address of q is\n"<<&q<<endl;
return 0;
}
[output]
value pointed by q is
0
value stored in q is
0xc202b8
address of q is
0x22cd14
value pointed by p is
0
value stored in p is
0xc202b8
address of p is
0x22cd18
address of p is
0x22cd18 //p still has a valid address in the memory
address of q is
0x22cd14 //q still has a valid address in the memory
[/output]
On a similar note, deleteoperator deallocates the memory and calls destructor(?) (Which , I assume, then destroys the memory
) Not sure what exactly does it mean by destroying the memory? Could somebody please explain the difference between de-allocation and destroying the memory? I have a vague idea but not sure about it.
Thanks in advance. Sorry if the question sounds silly. I am still in the learning phase of c++ :)
The address of the pointer remains unaffected by delete, and so does it value. The only thing that changes after calling delete is that the value of the pointer is an address pointing to a free memory location.
When we perform a delete operation on a pointer, we actually are deleting the value pointed by the pointer and not the pointer itself(?).
Yes.
I am just curious how this addresses are destroyed?
Assuming by addresses, you mean the pointer variables: Automatic objects (aka stack objects) like i, p and q are destroyed when leaving the scope they were declared in (so in this case, at the end of main).
Does this address, at some point in time, gets re-assigned to a newly declared pointer variable or does it just stay there in the memory?
Once an object is destroyed, its address can be reused for future objects.
Not sure what exactly does it mean by destroying the memory?
There isn't any memory being destroyed, only the object that q pointed to (an int) is destroyed and the memory it occupied is freed, meaning it can be used again for newly created objects.
@Bazzy - Thanks. Still some confusion, and so does it value. Not sure how do I interpret this statement. Could you please elaborate?
@Athar :
Assuming by addresses, you mean the pointer variables: Automatic objects (aka stack objects) like i, p and q are destroyed when leaving the scope they were declared in (so in this case, at the end of main). .
Nope. By address, I mean the actual address of the pointer variable itself(please refer to line number 43 of the output. Does this address ever gets destroyed? Because the next time I compile my program, pointer variable is still stored at the same address. I hope this explains what I want to ask. :)
Each process has its own address space, so the address 0x22cd18 can be a different physical address every time. When your program exits, all the memory it used is freed.