1

I have been looking in a sample code which i never expected to run.

class A
{
public: 
    void show() { cout << "show" << endl; }
};
int main()
{
    std::cout << "Hello World!\n"; 
    A * ptr = NULL;
    ptr->show();
    std::cout << "DONE!!!!!!";
}

I have declared a nullpointer which is still running and calling the show() method. How is it possible??

P.W
  • 26,289
  • 6
  • 39
  • 76
apb_developer
  • 321
  • 2
  • 9
  • 5
    [Undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior) can sometimes seem to work fine. Other times it causes crashes, or maybe even [nasal demons](http://www.catb.org/jargon/html/N/nasal-demons.html). – Some programmer dude Mar 15 '19 at 08:50
  • Unlike other languages, in C++ if you do silly things like this, then the behavior is undefined. – PaulMcKenzie Mar 15 '19 at 08:50
  • If you want to increase the chances of a crash, try using assigning to a member variable in the member function. – Some programmer dude Mar 15 '19 at 08:51
  • In fact it's running because the show method is neither a virtual function nor it access any attribute of your instance. The code generated does not use the null pointer. – Gojita Mar 15 '19 at 08:52
  • 1
    As for "why", imagine `ptr->show()` is rewritten as `__A_show(ptr)` (where ptr is the implicit this), and consider that `this`is never used. But this is a complete guess, compilers can do whatever they want, especially in face of UB. – coredump Mar 15 '19 at 08:52
  • 1
    So the undefined behavior and where the compiler is at liberty to do anything is the answer. Thankyou all for the answers... – apb_developer Mar 15 '19 at 09:02
  • @user463035818: The compiler can do anything it likes, so "compile as if the code does nothing" is certainly in the real of possibilities. Optimizations aren't even necessary for that. – MSalters Mar 15 '19 at 11:27
  • @MSalters they are not necessary, but I wouldnt expect a compiler to do such transformation without optimizations turned on – 463035818_is_not_an_ai Mar 15 '19 at 11:28
  • @MSalters ah now I get it. My wording was wrong/misleading. Comment deleted – 463035818_is_not_an_ai Mar 15 '19 at 11:30

3 Answers3

3

What you have here is undefined behavior because you are dereferencing a NULL pointer.

But if it runs, and prints "show", it is likely because:

  • The compiler does not check if the pointer is NULL before calling the function show
  • As the function show is not virtual, there is no need to refer to virtual tables to call it.
  • The function does not access any member variable in the class A so it doesn't need to dereference this pointer which is NULL in this case.
P.W
  • 26,289
  • 6
  • 39
  • 76
2

Well, it's, of course, an example of so-called undefined behavior what will happen depending on what the function of the object type you are calling does and how it might affect the state of your program. Different compilers can handle this in different ways etc. For your specific case, the reason most likely is that you actually have what you need in order to make the call. The type of the pointer is known, so the code for the method is known. The method doesn't use this or in any other way try to alter the object, so it runs the code just fine.

inquam
  • 12,664
  • 15
  • 61
  • 101
0

Since your 'show' method is a member function it is implicitly given pointer to an object as an argument. Since your class does not have any members that would require this pointer to be accessed and 'show' method does not do anything with it - all works. Your implicit pointer to your object is just ignored. You may also do this:

void show(void *ptr)
{
    std::cout << "wow it works - I wonder why ;-)" << std::endl;
}

and invoke it like:

show(NULL);

It will work but it does not mean you should do such things ;-)

Artur
  • 7,038
  • 2
  • 25
  • 39
  • i dont like your exaple too much, because in contrast to OPs code there really is nothing wrong with your example while OPs code indeed has undefined behaviour and is doomed to fail sooner or later – 463035818_is_not_an_ai Mar 15 '19 at 09:16
  • This is just to show why it works and not to recommend what he did. He asked "How is it possible??" – Artur Mar 15 '19 at 09:29
  • yes I also realized that and you answer that, though a complete answer should also mention that OPs code only seems to work but one should never write such code (which isnt the case for your example) – 463035818_is_not_an_ai Mar 15 '19 at 09:32