What do you expect from printing?
#include <iostream> #include <string> int main() { int* test = new int[20]; test[15] = 5; std::cout << test[15] << "\n"; delete[] test; std::cout << test[15] << "\n"; return 0; }
In release mode on VS 2010, I get this result:
5
5
Unallocated memory does not necessarily throw an exception when trying to access it. The value does not have to be rewritten. (Interestingly, if I changed the compilation mode to debug mode, the compiler overwrites the value with -572662307 ).
What happens when you try to access it is undefined by standard. This means that the compiler can do anything, or do nothing. (Or crashing your program or blowing up the universe ...)
Merlyn morgan-graham
source share