Why the program not crash when access NULL object?

vql
Go4Expert Member
12Oct2007,20:36   #1
vql's Avatar
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.
Go4Expert Founder
12Oct2007,22:19   #2
shabbir's Avatar
Because you compiler does find that function where p points.
Team Leader
12Oct2007,22:46   #3
DaWei's Avatar
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.
vql
Go4Expert Member
13Oct2007,15:37   #4
vql's Avatar
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.
Go4Expert Founder
13Oct2007,18:12   #5
shabbir's Avatar
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