-2

I have this code:

#include <iostream>

using namespace std;

void main(){
    int *ptr = new int(15);

    cout << "Address of ptr: " << ptr << endl;
    cout << "Content of ptr: " << *ptr << endl << endl;

    delete ptr;
    *ptr = 30;

    cout << "Address of ptr: " << ptr << endl;
    cout << "Content of ptr: " << *ptr << endl;
}

And this is the output:

Address of ptr: 007B81F0
Content of ptr: 15

Address of ptr: 007B81F0
Content of ptr: 30

Why does this work? Why can I still use the pointer? What happened?

Is this useful at some point?

phez1
  • 1,477
  • 3
  • 14
  • 19
  • 10
    It *doesn't* work. "Work" doesn't mean what you think it does. – Kerrek SB May 21 '15 at 17:55
  • 1
    It looks like working code... but it really doesn't. Try to "malloc" some new memory for other resources and your code will overwrite the allocated memory from the other resources. Simply: Your code is "undefined behaviour". All can happen, from "looks like working" to crash. – Klaus May 21 '15 at 17:57
  • 1
    Just allocate some more memory before `*ptr = 30;` and try and see what happens. – Abhishek Vasisht May 21 '15 at 17:58
  • 2
    This is a good example of [Undefined Behavior](http://en.wikipedia.org/wiki/Undefined_behavior). In this case, the code is *WRONG*. It might *appear* to work (in the sense it might not crash on your particular system) ... but that doesn't make it "correct". And it doesn't necessarily mean it will continue to work on a different system, or in a different context. Just "don't do it" ;) – paulsm4 May 21 '15 at 17:58
  • 2
    You should read this: http://stackoverflow.com/a/6445794/4342498 – NathanOliver May 21 '15 at 18:03
  • @NathanOliver that answer is one of the best I've ever seen dealing with explaining some concept. – vsoftco May 21 '15 at 18:06
  • @vsoftco That answers makes we want to be able to favorite answers just so I can favorite it. – NathanOliver May 21 '15 at 18:08

3 Answers3

11

This is typical undefined behaviour, and it happens to work because the memory that was previously allocated for *ptr was not yet reclaimed by the operating system*. You should never rely on this kind of code.

Note that the address of the pointer stays the same, as C++ runtime doesn't bother to nullify the pointer (it takes time and in C++ you don't pay for what you don't need).

*see also Remy Lebeau's comment below.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • 3
    Also keep in mind that some RTLs cache freed memory for later re-use. So the memory may still be physically allocated even if it has "logically" been freed. But that is an implementation detail, don't rely on it. If you `delete` memory, assume you don't have access to it anymore. – Remy Lebeau May 21 '15 at 17:57
0

Just as vsoftco explained, the memory was not yet reclaimed by the OS. Now if we do something like below, you may experience a completely different output:

int *ptr = new int(15);

cout << "Address of ptr: " << ptr << endl;
cout << "Content of ptr: " << *ptr << endl << endl;
delete ptr;

int *ptr2 = new int(15); // making a new ptr to use memory.
*ptr = 30;               // <- OPS!
delete ptr2;             // deleting new ptr.

cout << "Address of ptr: " << ptr << endl;
cout << "Content of ptr: " << *ptr << endl;

Anyway, the result is undefined behaviour

Andreas DM
  • 10,685
  • 6
  • 35
  • 62
-3

Although delete key word was used but for some reason de-allocation of pointer did not happen.(Probably scope of the pointer was still in use)

Using pointers is an important aspect of C++.It is important to use pointers to deal at reference level or with memory addresses.

frank
  • 1
  • 1