Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Why the program not crash when access NULL object? (http://www.go4expert.com/forums/program-crash-access-null-object-t6805/)

vql 12Oct2007 20:36

Why the program not crash when access NULL object?
 
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.

shabbir 12Oct2007 22:19

Re: Why the program not crash when access NULL object?
 
Because you compiler does find that function where p points.

DaWei 12Oct2007 22:46

Re: Why the program not crash when access NULL object?
 
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 13Oct2007 15:37

Re: Why the program not crash when access NULL object?
 
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.

shabbir 13Oct2007 18:12

Re: Why the program not crash when access NULL object?
 
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


All times are GMT +5.5. The time now is 00:20.