0

I try to delete a ptr using the following code example:

int* data = new int(1);

int* p = NULL; 

p = data;

*p = 3;

delete p;

// Prints 3
cout << *p << endl;

//Prints 3    
cout << *data << endl;

The last two lines of code prints 3, and my question is what is deleted after delete p? According to what it prints out from the last two lines of code, it seems like nothing is deleted 0.0~~

Someone please explain it to me. Thank you all in advance.

iammilind
  • 68,093
  • 33
  • 169
  • 336
Mocha
  • 21
  • 6
  • 1
    What happens is not defined by the Standard. As far as C++ is concerned the address is invalid and should not be used. What happens if you use it anyway is anybody's guess. – user4581301 Feb 13 '19 at 05:45
  • 2
    What typically happens is that the chunk of memory the pointer pointed to is added back into the process's available-memory-list, which means that it may be handed out to other code in your process via a future call to `new`/`malloc()`/etc. It's not safe to read or write that memory after the delete call (both because some other thread might be reusing it already, and more formally because it invokes undefined behavior, which means the compiler is free to assume you will never do that, and therefore may happily allow strange/unpleasant/mysterious things to happen if you do) – Jeremy Friesner Feb 13 '19 at 05:50
  • Once you delete a pointer, using it for anything will result in nasal demons. I've heard these can be quite painful. But some people don't even notice them, until they get angry one day for no discernible reason and stab the inside of your nose with their little pitchforks. – Omnifarious Feb 13 '19 at 06:14

2 Answers2

3

Dereferencing a pointer after you have deleted the object is cause for undefined behavior. Don't count on any predictable behavior and avoid it.

An exceptional read on dangling pointers resulting from a differect angle and undefined behavior: Can a local variable's memory be accessed outside its scope?

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Then both `p` and `data` become "un-predictable" or only 'p' itself becomes "un-predictable"??? What dose it mean by "becoming `un-predictable or predictable`"??? – Mocha Feb 13 '19 at 05:53
  • @Mocha, you should not dereference either of those variables. – R Sahu Feb 13 '19 at 05:54
  • Then it will just print out the memory addresses of those pointers~ – Mocha Feb 13 '19 at 05:59
  • @Mocha, I didn't follow that comment. – R Sahu Feb 13 '19 at 06:00
  • okay I see. since it is going to be marked as "unused" memory. people wouldn't try to use it after "delete". Then is it safe for me to say it is the memory address of `p` getting marked for unused when I `delete p`. The memory address of `data` isn't getting marked for unused. – Mocha Feb 13 '19 at 06:10
  • @Mocha, Whether you use `delete p;` or `delete data;` does not make a difference since both point to the same object. Regardless of which one you use, both `p` and `data` become dangling pointers. Dereferencing either one is cause of undefined behavior. Nothing is marked as unused. As a developer, you'll have to keep track what you are doing with dynamically allocated memory and make sure that you don't dereference any dangling pointers. – R Sahu Feb 13 '19 at 06:15
1

What happens is not defined by the Standard. As far as C++ is concerned the address is invalid and should not be used. What happens if you use it anyway is anybody's guess.

In practice, that memory is usually sitting there until something else needs it. All you've done is said, "I don't need it anymore." You can still access it for a while afterward, leading you to make very bad assumptions about the fitness of your code, but it can be reclaimed and reassigned at any time.

If the process needs memory for something else, maybe it gets that which was pointed at by data. In this case using data could corrupt memory used by another part of your program. This is a really bad scene because it's hard to track down what really happened when something totally unrelated to the bug crashes the program.

If another process needs memory, maybe the underlying system takes the memory back and gives it to the other process. On a modern PC accessing data after the memory's been given to the other process will be fatal.

user4581301
  • 33,082
  • 7
  • 33
  • 54