It's time to learn what undefined behavior is. :)
In C++, when you do something illegal/nonsensical/bad/etc. the standard often says that "it leads to undefined behavior." This means that from that point forward, the state of your program is completely non-guaranteed, and anything could happen.
At the point where you do your last *(pTest)
, you get undefined behavior. This is because pTest
does not point to a valid object, and dereferencing such a pointer is undefined. So what you're seeing is totally allowed: undefined output.
All you've done is said "I'm finished with this allocation." Once you've said that, you shouldn't (and indeed, cannot) inspect or care about that memory any longer. It doesn't even make conceptual sense to deallocate something then try to use it; you've said you were done!
Your output is somewhat predictable though: likely, your OS simply says "okay, thanks for the memory" and that's it. It has no reason to actually "reset" the memory, or do anything special. That would indeed be a waste of time, when nobody (including your own program) is not using it.
But remember, this output is completely undefined. Don't try to use objects that don't exist. Perhaps a better test would have been:
#include <iostream>
struct foo
{
~foo()
{
std::cout << "foo is gone :(" << std::endl;
}
};
int main(void)
{
foo* f = new foo();
delete f; // you'll see that the object is destroyed.
}
Although it seems you were looking to see what happens with the memory itself. Just remember that it makes no sense to get rid of memory then try to use it, so the answer is: who knows. It's up to your specific platform, which C++ doesn't care about.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…