Why Doesn't the Program Crash When I Call a Member Function Through a Null Pointer in C++

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

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.

Why calling function with nullPtr does not crash my application?

This is a common thing in C++, the function is not part of the instance, but part of the class definition.

If you tried to access this in the function then you would have a crash.

As @YSC mentioned below, this is considered undefined behavior, and you should not assume this will work. but it will mostly work and i heard this is even asked in C++ interviews questions.

What will happen when I call a member function on a NULL object pointer?

It's undefined behavior, so anything might happen.

A possible result would be that it just prints "fun" since the method doesn't access any member variables of the object it is called on (the memory where the object supposedly lives doesn't need to be accessed, so access violations don't necessarily occur).

Why my program does not crashes when I assign pointer to NULL?

You can visualize your method as a function:

int funct(MyClass *this) { return 0; }

You can see that the function doesn't use this at all so it won't crash if it is 0.

However, don't do such things in real code. This is still an Undefined Behavior and compiler optimizations can mess it up.

Why can I call a member function through a wild pointer

Short answer:
"start()" is a non-virtual function.

For non-virtual functions, C++ determines what function it calls depending on the pointer
type - not on the type of the actual refereced object.

So it finds the function via the pointer type and then, as you don't use the referenced object in "start()" (you don't access any data members), your program doesn't produce a segmentation fault.

If you call a virtual member function such as "stop()", however, C++ uses dynamic dispatch to determine the function to be used.
Therefore it tries to get a pointer to the virtual method table from the object and this results in the segmentation fault.

Pointer still able to call member function, after it was set to NULL and delete being called on it

If a program accesses an object outside of its lifetime, then the behaviour of the program is undefined. Your program accesses an object outside of its lifetime. The behaviour of your program is undefined. The behaviour that you observed is because the behaviour is undefined. You could have observed any other behaviour because it is undefined, but you didn't observe other behaviour because it's undefined.

Related Topics

Leave a reply