lundi 20 avril 2015

Simple C++ Nullptr Program Confusing Result

Consider the program below

#include <iostream>

using namespace std;

int main(){

  int *x = nullptr;

  x = new int(13);

  if(x)
    cout << "Hi" << endl;
  else
    cout << "Bye" << endl;

  delete x;

  return 0;
}

Output: Hi

I don't understand why "Hi" is output if the pointer is not null and points to someplace in memory. Shouldn't "Bye" be output? Any guidance would be appreciated

Aucun commentaire:

Enregistrer un commentaire