To my surprise, my program did not crash when I called display() by NULL pointer, but it crashed when I tried to access member variable.
#include
#include
using namespace std;
class Test
{
public:
int p;
void display() {
cout << "Test::display()" << endl;
};
};
int main()
{
try {
Test *t = NULL;
t->display();
t->p = 5;
} catch (exception &e) {
cout << "Dereferencing pointer: " << e.what();
} catch (...) {
cout << "Unknown Exception";
}
return 0;
}
#include
#include
using namespace std;
class Test
{
public:
int p;
void display() {
cout << "Test::display()" << endl;
};
};
int main()
{
try {
Test *t = NULL;
t->display();
t->p = 5;
} catch (exception &e) {
cout << "Dereferencing pointer: " << e.what();
} catch (...) {
cout << "Unknown Exception";
}
return 0;
}
1 comment:
Text book explanation would be that it should exhibit arbit behavior based on where the pointer is pointing to ... since you are explicitly pointing to null and it still executes I think compiler implementation are to be blamed ... Btw post vegayan I had the opportunity to code a lot in Python too and I have come to the conclusion that C++ is one shitty over bloated language ... Perl is dying right now and I would be happy if C++ goes the same route ...
Post a Comment