int main()
{
  int *ptr=new int (100);
   cout<<"p's add is "<<ptr<<endl;
   cout<<"value in p is"<<*ptr<<endl;

   delete ptr;    // deallocation of ptr;

   cout<<"p's add is "<<ptr<<endl;
   cout<<"value in p is"<<*ptr<<endl;

    return 0;
}

Even the delete operator is executed, the last two output statements are working same the first two output statements..

How is this possible...?????????????????

Recommended Answers

All 5 Replies

Above code will never work because 'p' is undeclared. Did you mean ptr?

ps. Your questionmark-key appears to be broke. You should buy a new keyboard. :icon_wink:

[edit]
Also read this before you post code again

Above code will never work because 'p' is undeclared. Did you mean ptr?

ps. Your questionmark-key appears to be broke. You should buy a new keyboard. :icon_wink:

[edit]
Also read this before you post code again

Sorry,, It is "ptr" not "p"

Even the delete operator is executed, the last two output statements are working same the first two output statements..

How is this possible...?????????????????

When you free memory, it may or may not be reused immediately for something else. If it is not reused, you can still access the pointer and get what was there before. But it is not safe to rely on that behavior because there is no way to tell if the memory was reused. The only way to guarantee that you own the memory is not to free it until you are really done with it.

Technically your code invokes undefined behavior because you use a pointer to access memory after it was freed. Undefined behavior is very bad because it makes your program totally unpredictable.

>Even the delete operator is executed, the last two output statements are working
>same the first two output statements.
This is implementation specific. On my implementation the two outputs are different:

siddhant3s@Xion:~$ g++ tester.cpp -otester.exe &&./tester.exe
p's add is 0x804a008
value in p is100
p's add is 0x804a008
value in p is0

Actually, after you do the "delete ptr", you can never be assure about what is the value of the memory chunk, ptr was pointing to.
An interesting point to note is by considering the following :

int main()
{
int *ptr=new int (100);
cout<<"p's add is "<<ptr<<endl;
cout<<"value in p is"<<*ptr<<endl;

delete ptr; // deallocation of ptr;

cout<<"p's add is "<<ptr<<endl;
cout<<"value in p is"<<*ptr<<endl;

int* ptr2=new int(2);
cout<<"p2's add is "<<ptr2<<endl;
cout<<"value in p2 is"<<*ptr2<<endl;


//    siddhant3s:~$ g++ tester.cpp -otester.exe &&./tester.exe
//    p's add is 0x804a008
//    value in p is100
//    p's add is 0x804a008
//    value in p is0
//    p2's add is 0x804a008
//    value in p2 is2
}

So, look what my compiler did. As I said "delete ptr", it zero out the value pointed by the pointer but did not changed the ptr's value itself. It was still 0x804a008. Now when I requested him to allocate another memory chunk to ptr2, he smartly allocated that old chunk which I deleted.
If I hadn't deleted ptr, the output would come something like this:

p's add is 0x804a008
value in p is100
p's add is 0x804a008
value in p is100
p2's add is 0x804a018
value in p2 is2

This time the compiler had to allocate a new memory chunk because the ptr was not freed at that moment.

Moral of the Story: You really cannot tell what happens to the value of the memory chunk pointed by the deleted pointer. So don't rely on it.

commented: Solid explanation. +7

Thank Q!!

Thank Q for our Reply..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.