Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't the program crash when I call a member function through a null pointer in C++?

#include "iostream"
using namespace std;
class A
{
public:
    void mprint()
    {
        cout<<"\n TESTING NULL POINTER";
    }
};

int main()
{
    A *a = NULL;
    a->mprint();
    return 0;
}

I am getting output as "TESTING NULL POINTER". Can anyone please explain why this program is printing the output instead of crashing. I checked it on Dev C++ and aCC compiler both gave same result.

like image 556
user258367 Avatar asked Mar 25 '11 10:03

user258367


People also ask

What happens when we try to access null pointer in C?

Because a null pointer does not point to a meaningful object, an attempt to dereference (i.e., access the data stored at that memory location) a null pointer usually (but not always) causes a run-time error or immediate program crash. In C, dereferencing a null pointer is undefined behavior.

Which of the following will occur if we call the function on the null pointer?

Typically, if you call free() with a null or an invalid pointer, a function that is written defensively will not corrupt the heap storage, but will instead do nothing.

What happens when a pointer is set to null?

Explanation: What happens here is that when a Null pointer is created, it points to null, without any doubt. But the variable of Null pointer takes some memory. Hence when a pointer to a null pointer is created, it points to an actual memory space, which in turn points to null.

Can you call a function from a Nullptr?

Calling a member function on a NULL object pointer in C++A class member function can be called using a NULL object pointer. Note − This is undefined behaviour and there is no guarantee about the execution of the program.


2 Answers

You're not using any member variables of A - the function is completely independent of the A instance, and therefore the generated code happens to not contain anything that dereferences 0. This is still undefined behavior - it just may happen to work on some compilers. Undefined behavior means "anything can happen" - including that the program happens to work as the programmer expected.

If you e.g. make mprint virtual you may get a crash - or you may not get one if the compiler sees that it doesn't really need a vtable.

If you add a member variable to A and print this, you will get a crash.

like image 120
Erik Avatar answered Sep 19 '22 18:09

Erik


According to the C++ spec, This program has undefined behavior because you're invoking a member function on a null receiver.

The reason that this works, though, is that non virtual member functions are typically implemented as regular functions that take the "this" pointer as an implicit first argument. Consequently, if you call a member function on a null pointer, as long as you don't use the this pointer, your program will not crash. Of course, you cannot rely n this; a valid C++ compiler could cause this to crash.

However, virtual functions are a different story because the function that actually gets called needs to be resolved at runtime. This usually involves introspecting on the receiver's virtual function table. Thus if you try calling a virtual member function on a null pointer, even if te function doesn't access this, it will still cause a crash. Try this out if you're curious!

like image 34
templatetypedef Avatar answered Sep 19 '22 18:09

templatetypedef