title: Pointer null check evaluates to true even though it is not assigned a null value
body: <p>This is a code for creating a binary tree, and it is executing perfectly. What I don't understand is how <code>temp-&gt;left==NULL</code> or <code>temp-&gt;right==NULL</code> conditions for the <code>if()</code> statements inside the <code>insert(string)</code> function are getting evaluated to <code>TRUE</code> when, in my opinion, it should be throwing a segmentation fault since nowhere have I initialized the <code>left</code> or <code>right</code> pointers of a node to <code>NULL</code>.</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  class node
  {
    public:
      string data;
      node *left,*right;
  };
  
  class btree
  {
    private:
      node *root;
  
    public:
      btree()
      {
        root=NULL;
      }
      void create();
      void insert(string);
  };
  
  void btree:: create()
  {
    cout&lt;&lt;"\n\nEnter the no. of nodes you want to create: ";
    int n;
    cin&gt;&gt;n;
    cin.ignore();
    for(int i=0;i&lt;n;i++)
    {
      cout&lt;&lt;"\nEnter data for node "&lt;&lt;i+1&lt;&lt;": ";
      string input;
      getline(cin,input);
      insert(input);
    }
  }
  
  void btree:: insert(string str)
  {
    if(root==NULL)
    {
      root=new node;
      root-&gt;data=str;
      return;
    }
    node *temp=root;
    while(temp!=NULL)
    {
      cout&lt;&lt;"\n\nDo you want to enter the node in the left subtree or the right subtree of "&lt;&lt;temp-&gt;data&lt;&lt;"?(l/r): ";
      char dir;
      cin&gt;&gt;dir;
      cin.ignore();
      if(dir=='l')
      {
        if(temp-&gt;left==NULL)
        {
          temp-&gt;left=new node;
          temp-&gt;left-&gt;data=str;
          return;
        }
        else
          temp=temp-&gt;left;
      }
      if(dir=='r')
      {
        if(temp-&gt;right==NULL)
        {
          temp-&gt;right=new node;
          temp-&gt;right-&gt;data=str;
          return;
        }
        else
          temp=temp-&gt;right;
      }
    }
  }
  
  int main()
  {
    btree bt;
    bt.create();
  
    return 0;
  }
  </code></pre>
  
  <p>Here is the output:</p>
  
  <pre><code>Enter the no. of nodes you want to create: 3
  
  Enter data for node 1: 5
  
  Enter data for node 2: 1
  
  
  Do you want to enter the node in the left subtree or the right subtree of 5?(l/r): l
  
  Enter data for node 3: 9
  
  
  Do you want to enter the node in the left subtree or the right subtree of 5?(l/r): r
  </code></pre>
  
  <p>To check whether this is indeed just UB or an effect of the <code>new</code> operator I ran a test code:</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  class node
  {
    public:
      int data;
      node *left,*right;
  };
  
  int main()
  {
    node *root=new node;
    root=NULL;
    cout&lt;&lt;"\nroot = "&lt;&lt;root; //This cout is executed since root has been initialized to NULL 
    std::cout.flush();
    cout&lt;&lt;"\nroot-&gt;left = "&lt;&lt;root-&gt;left; //This line immediately throws segmentation fault
    std::cout.flush();
  
    return 0;
  }
  </code></pre>
  
  <p>Output of the test code:</p>
  
  <pre><code>root = 0
  Segmentation fault (core dumped)
  </code></pre>
  
  <p>So the test code seems to recognise UB and throws segfault. Why not the first code?</p>
  
  <p>I tried searching for a similar question but couldn't find one. </p>