Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
This is piece of code given by my mentor to analyse and state the reason behind its behaviour.
Being a beginner, I was a little surprised to see this code running fine, as pointer to the object of class has been set NULL.
I played with the code little, searched the web and came to a conclusion that the call to the function is being implicitly implemented as void funct(A* this) that is why it is working fine.

C++
#include<iostream>

using namespace std;

class A
{
    public:
        int a;
        void func (void)
        {
            cout << "hello world " <<endl;
        };
};

int main()
{
    A* ptr = NULL;
    ptr->func();
    return 0;
}


But my mentor told me that this is not the reason for this piece of code working fine and added if this is the reason then why does this code crashes. And added
"a" to the cout statement in the above code.
C++
void func (void)
{
     cout << "hello world " << a <<endl;
};


Now I am a little puzzled and not able to see it in any other way.
Can anyone provide any direction or any hint taking which I can work forward to solve it.
Posted

Memory of a class member function will be allocated at runtime, and all instance of the class will use the same function.

Accessing a member function which does not access a member variable will not cause an access violation.
Because there is no memory access related to the instance of the class.

C++
void A::func (void)
        {
            cout << "hello world " <<endl;
        }; 

will be compiled as

C++
void func (A* this)
        {
            cout << "hello world " <<endl; // this is no where accessed here.
        };

Since this in not accessed from func, calling func of class A will not create access violation.

But if func accesses any member variable, then it will create an access violation. The reason is memory access to a NULL pointer will happen, and it will create an access violation.

C++
void A::func (void)
        {
            cout << "hello world " <<a<<endl;
        }; 

will be compiled as

C++
void func (A* this)
        {
            cout << "hello world " <<this->a<<endl; // Here this is used and it will create access violation.
        };
 
Share this answer
 
v2
Comments
Member 8576081 14-Oct-12 5:27am    
Thanks for your reply!
To understand why this happens , you should know what is a class, what is an object.

In case of your class A,
if you try to execute the statement sizzeof(A) , then you will realize it will be the sum of size of data members

This itself mean that , only data members are part of any object and the corresponding functions are not.

Then how does any function accesses the data members if it is not a part of object ?

This is where famous this pointer comes into picture.

Hence if there is a code like

C++
A *ptr = new A();
ptr->func();


This code will be converted by compiler to

C++
A *ptr = new A();
func(ptr);   //Here ptr points to the object.


Hence your func function will access data members using the passed ptr (i.e. this pointer )

C++
void func( A* this )
{
printf("Some text");
}



if we chose to replace initialization of the ptr to NULL then we will be passing a NULL pointer to function.
C++
A *ptr = NULL;
ptr->func();


Compiler will convert this code to
C++
A *ptr = NULL;
func(ptr);


In func, if you try to access any data member
C++
A::func()
{
printf( "a = %d", a);
}


compiler will convert this code to
C++
func( A* this )
{
   printf("a = %d", this->a);
}


Now you can observe that , as we are sending NULL pointer to the function. It has to crash :)
 
Share this answer
 
Comments
Member 8576081 16-Oct-12 3:40am    
My 5! Thanks for taking out time!
read this link[^] in stack overflow it will clear up lots of issue and also raise lots of question in your mind too :)
 
Share this answer
 
Comments
Member 8576081 14-Oct-12 5:28am    
Thanks for confusing me even more :) ;) .. Cheers!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900