How address assigned to a pointer is actually destroyed?

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 :(

Following is the program I have tried.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
$ cat dangling_ptr.cpp
#include<iostream>
using namespace std;

int main()
{
  int i=10;
  int* p;
  int* q = new int;

  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, delete operator 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. :)
Does this address ever gets destroyed?

You cannot destroy addresses, they're just numbers. However, you can free the memory at that address, if that's what you mean.

Because the next time I compile my program, pointer variable is still stored at the same address.

I take it you are not familiar with virtual memory:
http://en.wikipedia.org/wiki/Virtual_memory

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.
Each process has its own address space, so the address 0x22cd18 can be a different physical address every time

Oh. Right. Now it clears the things :D I Didn't think of it that way. Thanks a lot buddy!
Topic archived. No new replies allowed.