Although the prototype for memberFunction is
void memberFunction( int arg_param )
what really happens beneath the scenes is the compiler passes a second parameter to this function, that parameter being a pointer to the instance of A that you are calling the member function on. (The so-called "this" pointer, essentially). Because memberFunction doesn't actually look at any data members of A, the pointer is not derefenced; it is simply unused. This is why the code above does not crash. This also explains why it does crash if you attempt to print m_memberVariableX, because now you are attempting to dereference the "this" pointer to access the member variable.
You should find that another way to crash this program is to make memberFunction (as it is now) a virtual function. Even though it still doesn't access any data members, it still crashes. |
yes I dig that thread to the death , Thanks a lot jsmith. he has explained this very great , but that is a logical explanation . I need now physical explanation ,
How this code will be physically implemented ? what causes that error to happen . I know why that error happens , thus because he accessed a memory location that not belongs to the process or thread .
This is the exception informations that I have got.
Exception Information
code: 0x00000005 Flags : 0x00000000
Address : 0x000000000000401625
well using this information , I go to the debug this program , and I got the
breakpoint at that address .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
18: cout << a <<" " << b << endl ;
0040161D push offset @ILT+205(std::endl) (004010d2)
00401622 mov eax,dword ptr [ebp-4]
00401625 mov ecx,dword ptr [eax+4]
00401628 push ecx
00401629 push offset string " " (0046c044)
0040162E mov edx,dword ptr [ebp-4]
00401631 mov eax,dword ptr [edx]
00401633 push eax
00401634 mov ecx,offset std::cout (004777e0)
00401639 call @ILT+260(std::basic_ostream<char,std::char_traits<char> >::operator<<) (00401109)
0040163E push eax
0040163F call @ILT+650(std::operator<<) (0040128f)
00401644 add esp,8
00401647 mov ecx,eax
00401649 call @ILT+260(std::basic_ostream<char,std::char_traits<char> >::operator<<) (00401109)
0040164E mov ecx,eax
00401650 call @ILT+485(std::basic_ostream<char,std::char_traits<cha
|
Oky here as I underestood , the object address is passing as a parameter to the function print it is @ [ebp-4] ( just getting that out of the stack) , so it put to the eax , and is 0 ( because I pass NULL no ) , and the next instruction is
ecx,dword ptr [eax+4]
is that refering to the b ? am I correct , then the instructions , .....
0040162E mov edx,dword ptr [ebp-4]
00401631 mov eax,dword ptr [edx]
are accessing the variable a .....
wow man , this is great ! So there is a way and a well definided procedure that compiler generates the object code . I need to know more . Pls help .
some hyperlinks to read is greater .
thankx man.