After I searched about how to verify if a pointer is deleted in C++, i found out that there is no certain way, some really ugly work-around or smart pointers (I first want to understand how normal pointers works). My question is why C++ crashes when I try in a try/catch to show the value of a deleted pointer? shouldn't it handle the crash and just print the exception ??
void main()
{
int *a = new int[5];
for (int i = 0; i < 5; i++)
{
*(a + i) = i;
}
for (int i = 0; i < 5; i++)
{
if (*(a + i) != NULL) // useless verify, cuz if it would be NULL, it
//would crash imediately
{
cout << (a + i) << ", " << *(a + i) << endl;
}
}
delete a;
cout << a << ", ";// << *a << endl;
for (int i = 0; i < 5; i++)
{
try{
cout << (a + i) << ", " << *(a + i) << endl;
}
catch (int e)
{
cout << "Error: " << e << endl;
}
}
cin.get();
}