Hi guys I'm learning about memory,and I was expecting this program to crash as I allocated memory in the heap to a pointer but I then deallocated the memory yet when I do cout << hi; or cout << *hi; the program does not crash how come the program didn't crash like I expected it would?
I'm not sure what you mean cire,there's always a chance but it's not guaranteed but how would that be any way related to my program crashing vs not crashing?
ohh ok,so that means theres a chance that hi would still be allocated to an address?
I don't know what that means. hi is a pointer. It will always contain an address. It will be "allocated" as long as it is in scope. Whatever it was pointing to stopped existing when you called delete on hi. It is entirely possible, however, that the memory at the address contained by hi still contains the bit pattern that it did before it was deleted.
It is illegal to access it, however, and you certainly couldn't count on that behavior being consistent.
According to the standard, you're in the realm of undefined behavior where anything could happen. The program could crash, nuclear missiles could be launched or nothing might happen but your program continuing innocuously on, biding its time, waiting to bite you on the ass at some point in the not-so-distant future.
Would now be a good time to mention that it is best to completely avoid new and delete? They are low level memory management functions. Also avoid raw pointers, there are smart pointers which are much safer to use. But you might not need them for awhile. While I am at it, one doesn't need char arrays, or arrays in general. One can do awesome amounts of stuff just using the STL and not worry about memory management at all.