CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2007
    Posts
    249

    Exclamation calling mem ber function by object pointer

    Hi,
    I have the following piece of code.
    Code:
    #include<iostream>
    using namespace std;
    
    class Test
    {
            public:
                    Test(){cout<<"Test"<<endl;}
                    void fun()
                    {
                            int i=5;
                            cout<<"fun"<<i<<endl;
                    }
    };
    int main()
    {
            Test *ptr = NULL;
            ptr->fun();
            return 0;
    }
    compiled with g++.
    Executing this give output fun5.

    It is correct? I have not allocated any object and so this pointer is not created.
    Then how it is able to run and call the function

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: calling mem ber function by object pointer

    Quote Originally Posted by Rajesh1978
    It is correct? I have not allocated any object and so this pointer is not created.
    Then how it is able to run and call the function
    The pointer is created: it is a null pointer. The code is obviously wrong: you're dereferencing a null pointer, hence the behaviour is undefined. As to why you did not observe any say, a crash or other indicator that something is wrong: it is probably because the object has no member variables. However, that does not make the code correct.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: calling mem ber function by object pointer

    Quote Originally Posted by Rajesh1978 View Post
    It is correct? I have not allocated any object and so this pointer is not created.
    Then how it is able to run and call the function
    With C++, there is no guarantee how your program will behave when you have code that is faulty. C++ isn't like other languages such as C# or Java where you have a stack trace or message box saying "you made a mistake".

    For example:
    Code:
    int main()
    {
        int array[100];
        array[100] = 0;
    }
    That is clearly out of bounds of the array, but will the "array[100] = 0" be executed without crashing? Maybe it will crash, maybe it won't. The same thing with returning pointers or references to local variables -- all of these aspects of C++ lead to undefined behaviour, even if the code is syntactically correct.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Jul 2007
    Posts
    249

    Re: calling mem ber function by object pointer

    Thanks u all

  5. #5
    Join Date
    Jul 2013
    Posts
    576

    Re: calling mem ber function by object pointer

    Quote Originally Posted by Rajesh1978 View Post
    Then how it is able to run and call the function
    The objects of a class don't store function code. This code is stored in one common place and is then shared by all objects. The objects store things that are specific for each object such as instance variables.

    In your case ptr is used only to identify the class to which fun() belongs and that's enougth to find the function and run it. The actual pointer value of ptr need never be used since nothing that's stored in objects is accessed. So it never becomes a problem that ptr is NULL.

    But say you do this,

    Code:
    class Test
    {
            public:
                    Test() {cout<<"Test"<<endl;}
                    void fun()
                    {
                            i=5;
                            cout<<"fun"<<i<<endl;
                    }
         int i; // i now belongs to the objects and not to fun()
    };
    Now the pointer value of ptr must be used to locate the i variable in the object but since it's NULL the program crashes.

    In both cases the code is wrong though. This just explains why "undefined behaviour" may look different in different cases.
    Last edited by razzle; November 30th, 2013 at 12:38 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured