Null Pointer Function Call
-
Plz See the following Example first. Class A { Void Func(){AfxMessageBox("Function Class A");} } int Main() { A *pClassA; pClassA=NULL; pClassA->Func(); } The output is a MessageBox displayed: AfxMessageBox("Function Class A"); Why this output is displayed because pointer has no memory plz explain this behaviour in the context of VC++ 6.0/MFC.Is this compliler Bug or My VC++6.0 is corrupt or any other.....? Yours Sincerely Ajmal Siddiqi ajmalsiddiqui -- modified at 6:45 Monday 8th May, 2006
-
Plz See the following Example first. Class A { Void Func(){AfxMessageBox("Function Class A");} } int Main() { A *pClassA; pClassA=NULL; pClassA->Func(); } The output is a MessageBox displayed: AfxMessageBox("Function Class A"); Why this output is displayed because pointer has no memory plz explain this behaviour in the context of VC++ 6.0/MFC.Is this compliler Bug or My VC++6.0 is corrupt or any other.....? Yours Sincerely Ajmal Siddiqi ajmalsiddiqui -- modified at 6:45 Monday 8th May, 2006
This works because: - The member function doesn't access any non-static members and thus doesn't use the (NULL)
this
pointer; and - Isn'tvirtual
so doesn't usethis
this to get a vtable pointer during function call. Steve -
Plz See the following Example first. Class A { Void Func(){AfxMessageBox("Function Class A");} } int Main() { A *pClassA; pClassA=NULL; pClassA->Func(); } The output is a MessageBox displayed: AfxMessageBox("Function Class A"); Why this output is displayed because pointer has no memory plz explain this behaviour in the context of VC++ 6.0/MFC.Is this compliler Bug or My VC++6.0 is corrupt or any other.....? Yours Sincerely Ajmal Siddiqi ajmalsiddiqui -- modified at 6:45 Monday 8th May, 2006
It is because classes don't have their own functions (they is only one function for each instances of the classes, and there is an implicit 'this' parameter that is passed to the function to identify the instance). Thus, the function exists even if the object doesn't exist. Now, if this function will access one of the member variable from the class, you will get an exception (because member variables are not 'shared') because you are trying to access invalid memory.
-
Plz See the following Example first. Class A { Void Func(){AfxMessageBox("Function Class A");} } int Main() { A *pClassA; pClassA=NULL; pClassA->Func(); } The output is a MessageBox displayed: AfxMessageBox("Function Class A"); Why this output is displayed because pointer has no memory plz explain this behaviour in the context of VC++ 6.0/MFC.Is this compliler Bug or My VC++6.0 is corrupt or any other.....? Yours Sincerely Ajmal Siddiqi ajmalsiddiqui -- modified at 6:45 Monday 8th May, 2006
If you look at the assembly code, you'll notice that the call to
Func()
is the same no matter ifpClassA
isNULL
or not.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"We will be known forever by the tracks we leave." - Native American Proverb