Because c++ never said said to erase deleted memory, delete just unallocates the memory. What you have there is a stray pointer, it points to a memory location that is no longer meaningful in it's previous context. If you were to have a much longer, and more complicated program, those values would eventually be overwritten, but for your program, nothing ever claims that memory and writes over it.
#include <iostream>
usingnamespace std;
int main()
{
// my little test
int i,n;
int * p;
i = 5;
p= new (nothrow) int[i];
if (p == 0){
cout <<"Error: memory could not be allocated";
}else{
p[0] = 123;
p[1] = 234;
p[2] = 345;
p[3] = 456;
p[4] = 567;
cout <<"Before delete:"<< endl;
cout << p << endl;
for (n=0; n<i; n++){
cout <<"p["<< n <<"]: "<< p[n] << endl;
}
delete[] p;
p= new (nothrow) int[i];
cout <<"\nAfter delete:"<< endl;
cout << p << endl;
for (int wtf=0; wtf<i; wtf++){
cout <<"p["<< wtf <<"]: "<< p[wtf] << endl;
}
}
delete[] p;
cout << *(new (nothrow) int[i]);
p = new (nothrow) int[i];
cout <<"\nAfter delete:"<< endl;
cout << p << endl;
for (int wtf=0; wtf<i; wtf++){ //You will see completely arbitrary values here
cout <<"p["<< wtf <<"]: "<< p[wtf] << endl;
}
// wait for the user to exit
shortint exitNumber;
cout <<"\n\nPress ctrl-c to exit.";
cin >> exitNumber;
// terminate the program
return 0;
}
If you wanted to, you could just make a pointer and read through memory until you hit a piece of memory that for one reason or other can't be read by your program
Yes, do not forget that delete [] p only marks the memory spaces p points to as deallocated. Since p may still contain the address of the deallocated memory, after typing delete [] p, you should type p=0, so that p doesnt point to them anymore.