title: Segmentation fault is coming due to constructor
body: <p>I wrote a C++ code to build binary search tree. Code compiled correctly, but when I try to run the executable its giving segmentation fault. Below is my code:</p>
  
  <pre><code>    #include &lt;iostream&gt;
  
  using namespace std;
  
  struct Node{
      Node *parent, *right, *left; 
      int data;
  };
  
  class bst{
      private:
      Node* root;
  
      public:
          bst (int data);
          void insert (Node * node, int data);
          void insert (int data);
          Node * search (int data);
          Node * search (Node * node, int data);
          // remove (Node * node, int data);
  };
  
  bst::bst (int data){
      root -&gt; data = data;
  }
  
  void bst::insert (Node * node, int data){
      if (node == NULL){
          node -&gt; data = data;
      }
      else if (data &lt; node -&gt; data){
          insert (node -&gt; left, data);
      }
      else if (data &gt; node -&gt; data){
          insert (node -&gt; right, data);
      }
  }
  
  void bst::insert (int data){
      insert (root, data);
  }
  
  Node * bst::search (Node * node, int data){
      if (node == NULL || node -&gt; data == data)
          return node;
      else if (data &lt; node -&gt; data)
          return search (node -&gt; left, data);
      else if (data &gt; node -&gt; data)
          return search (node -&gt; right, data);
  }
  
  Node * bst::search (int data){
      search (root, data);
  }
  
  int main(){
      cout &lt;&lt; "main entry\n";
      bst __bst (10);
      cout &lt;&lt; "new causing problem\n";
      bst bst2(1);
  //  bst *my_bst = new bst(10);
      cout &lt;&lt; "tree created\n";
  /*  my_bst -&gt; insert(32);
      my_bst -&gt; insert(3);
      my_bst -&gt; insert(36);
      my_bst -&gt; insert(93);
      my_bst -&gt; insert(23);
      cout &lt;&lt; "insertion completed\n";
  
      if (my_bst -&gt; search(4) == NULL )
          cout &lt;&lt; "4 does not exist\n";
      if (my_bst -&gt; search(36) != NULL)
          cout &lt;&lt; "36 exists in tree\n";
  */  return 0;
  }
  </code></pre>
  
  <p>While debugging for the problem, I cam across an interesting observation. When main function contains only <strong>bst __bst (10);</strong> object definition and <strong>"new causing problem\n"</strong> comment after that, it does not print anything. So I assumed that the first object definition is causing problems. But when I put <strong>bst bst2(1);</strong> or <strong>bst *my_bst = new bst(10);</strong> in the main, segmentation fault happens again but not before <strong>"new causing problem\n"</strong> is printed. So execution did go upto that point. So first object definition did not cause any problem in this case.</p>
  
  <p>Can someone tell what caused segmentation fault and why is this strange thing happening.</p>
  
  <p><strong>Edit:</strong>
  As all of you pointed out that I am trying to dereference a NULL pointer during constructor code, I did the suggested changes and code worked fine. But I have a similar program which is working even without those changes. Below is the code for that:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  struct Name{
      string first_name;
      string last_name;
  };
  
  class Person{
      public:
      Person (string first_name, string last_name){
              name -&gt; first_name = first_name;
              name -&gt; last_name = last_name;
      }   
  
      int get_age(){
              return age;
      }   
  
      void set_age (int new_age){
              age = new_age;
      }   
  
      string get_first_name(){
              return name -&gt; first_name;
      }   
  
      string get_last_name(){
              return name -&gt; last_name;
      }   
  
      private:
      Name * name;
      int age;
  };
  
  int main (){ 
      Person prince ("Prince", "Kumar");
      cout &lt;&lt; prince.get_first_name() &lt;&lt; endl;
      cout &lt;&lt; prince.get_age() &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>In this case the program runs perfectly fine. Did the default constructor for <code>Name</code> allocated some space to the <code>name</code> pointer. I knew that it allocates 0 values to numeric types and NULL to pointer, object and strings. Am I right?</p>