0

I was recently asked to predict the output of the following program in an interview:

#include <bits/stdc++.h>
using namespace std;

class a {
 public:
  int x = 10;
  void f() { cout << "hello" << endl; }

  void g() { cout << x << endl; }
};

int main() {
  a* ptr = new a();
  a* ptr1;
  a* ptr2 = NULL;
  ptr->f();
  ptr1->f();
  ptr2->f();

  ptr->g();
  ptr1->g();
  ptr2->g();
}

When function f() is called using Null and wild pointers, the output is hello, but when function g() is called using these two pointers, no output is seen on the console. What could be the reason of this behaviour?

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
Double A
  • 83
  • 1
  • 2
  • 4
  • 1
    Does this answer your question? [What are all the common undefined behaviours that a C++ programmer should know about?](https://stackoverflow.com/questions/367633/what-are-all-the-common-undefined-behaviours-that-a-c-programmer-should-know-a) – Yksisarvinen Feb 22 '20 at 16:22
  • 3
    Undefined Behaviour is *undefined*. Your code may whatever it pleases, including doing nothing, doing something, crashing, blowing up your computer or making [demons fly out of your nose](http://catb.org/jargon/html/N/nasal-demons.html) – Yksisarvinen Feb 22 '20 at 16:23
  • https://en.cppreference.com/w/cpp/language/ub – Jesper Juhl Feb 22 '20 at 16:25
  • 1
    Protected-mode operating systems were invented primarily to prevent code like this from causing too much havoc. The expected behavior is that the OS terminates the program, something you should see. – Hans Passant Feb 22 '20 at 16:30
  • On my machine, it generated 5 runtime errors and 1 SEGV crash. – Eljay Feb 22 '20 at 16:40

3 Answers3

3

The only valid answer - in my opinion - is: The program invokes Undefined Behaviour, so any result is valid. There's no way to tell what an arbitrary compiler will generate and the program in its entirety is invalid.

Additionally, please read Why should I not #include <bits/stdc++.h>?

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
1

The behavior of calling methods on uninitialized pointers and null pointers is undefined. That means you can no longer reason about the behavior of the program in any meaningful way. It could literally do anything. For example it could order pizza.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
0

Your program has undefined behavior. Having said that, there is a plausible explanation as to why the calls to f appear to work while the calls to g cause strange behavior.

The functions are many times conceptually translated as:

void mangled_function_for_f(a* const this)
{
   cout << "hello" << endl;
}

void mangled_function_for_g(a* const this)
{
   cout << this->x << endl;
}

Please note that this not used in f. Hence, the calls to f appear to be OK. Not so with g.

PS Don't count on my explanation on every platform or even the same platform with different compiler options. It's best to avoid code that causes undefined behavior.

R Sahu
  • 204,454
  • 14
  • 159
  • 270