Why the program not crash when access NULL object?

vql's Avatar, Join Date: Sep 2007
vql
Go4Expert Member
Quote:
#include <stdio.h>
class A {
public:
void PrintMe();
};

void A::PrintMe()
{
printf("Hello World\n");
}

void main()
{
A* p = 0;
p->PrintMe(); // why not crash
}
Please explain why the program is not crash when access to function PrintMe of object NULL. Please explain me clearly. Thanks.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Because you compiler does find that function where p points.
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Because your compiler is not standards compliant (neither is MS) in this respect.

Your use of the return type, "void", for main is also non-compliant.
0
vql's Avatar, Join Date: Sep 2007
vql
Go4Expert Member
Quote:
Originally Posted by shabbir
Because you compiler does find that function where p points.
I think you are wrong because p is NULL pointer. The function also have a address, where p points to. But compiler can not know p's address -> so compiler also don't know the function's address.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by vql
I think you are wrong because p is NULL pointer. The function also have a address, where p points to. But compiler can not know p's address -> so compiler also don't know the function's address.
If you are using MS compiler try using an F11 at the function call and see what happens