In delete p; why p[1] is deleted? |
There is no way to know what has been deleted from the bits you posted. deleted does not mean "reset to 0". A variable can be reset to 0 without being deleted, and can be deleted without being reset to 0.
So my answer to this question is: it might not be deleted. You have no way to know.
BOTH Give the same output. WHY??? |
Rehan: It's like ne555 said. Undefined behavior is undefined.
If you allocate with
new
you must clean up with
delete
. If you allocate with
new[]
you must clean up with
delete[]
Only if you do that, are the results predictable and correct.
If you stray from that, you get into area that's "undefined" which means the compiler is free to do whatever it wants. It might delete all of it, it might delete only the first element, it might delete none of it. It might crash your program, or it might write a text file to the disk telling you that you're a bad programmer. Anything would be "legal" because it's undefined.
The new[] and delete[] pairing of this code snippit:
http://liveworkspace.org/code/3XHEdL$2
Is correct. Whereas the new[]/delete pairing of the other code snippit is undefined ... therefore "anything goes".
However both snippits are accessing bad memory on line 11 so you're doing undefined behavior in both of them.