Hi All,

I got this code from a forum & tried it on g++/Linux.
In the code below i put NULL to a pointer variable but didnt get any errors.

#include <iostream>
using namespace std;

class A
{
private:
   int _dmember;

public:
   void func()
   {
     cout<<"Inside A!! "<<endl;
     cout<<_dmember; // 
   }
};

int main ()

{

    A *a=NULL;

    a->func(); // prints "Inside A!!!" 

    return 1;
}

can someone please explain this to me ?

Does it work? It prints "Inside A!!!" but does it print a number on the next line? If not then your program silently crashed.

It can get into A::func and print the first line (which is what my windows implementation does before reporting a crash at the next line) because up to that point it has not had to dereference this. It is only at line 13 when trying to access _dmember that it deferences this which is NULL and crashes.

This is all undefined behaviour so in theory it could run and print out you age (don't ask me how it got your age, the behaviour is undefined remember, it can do anything, perhaps it dialed into some government agency hacked their computers and got a copy of your birth certificate). The import thing to remember here is always avoid undefined behaviour, it's bad and it doesn't necessarily show up in testing, only after it has been deployed for 2 months.

thanks Banfa.

*a

can only hold the memory address and it is NULL.
when i dereferenced

a->func(); 

it didn't crash why ?

Try this and let me know If you get the second cout statement

#include <iostream>
using namespace std;

class A
{
private:
   int _dmember;

public:
   void func()
   {
     cout<<"Inside A!! "<<endl;
     cout<<_dmember; // 
   }
};

int main ()

{

    A *a=NULL;

    a->func(); // prints "Inside A!!!" 

    cout << "We are now outside a->func().  If you can see this then your program didnt crash

    return 1;
}
commented: yes it crashed +2

This is a quirk of the C++ object model. Since the member function isn't virtual, the compiler knows exactly what function to call and doesn't depend on the object instance being valid. Technically, all non-virtual member functions are shared under the hood (virtual member functions work through a lookup table stored in the object). When you say a->func(), the generated code looks more like this:

struct A
{
    int _dmember;
};

void __class_A_12345_func(A *self)
{
    cout << "Inside A!! " << endl;
    cout << self->_dmember;
}

...

int main()
{
    A *a = NULL;

    __class_A_12345_func(a);

    return 1;
}

Given a low level view of how things are set up, it's easy to see how a->func() will work just peachy when a is a null pointer. However, the problem is inside the function where you request the value of an instance data member, which invokes undefined behavior because a is a null pointer. I'd expect a crash at that point, but your compiler may generate code that supresses it for this case. Regardless, the code is broken because the function tries to access something that relies on a valid instance.

thanks deceptikon.

I played a bit more with this little piece of code.

I modified the code as follows (i put _dmember first):

         void func()
            {
            cout<<_dmember;
            cout<<"Inside A!! "<<endl;
            }

this time it crashed without printing something.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.