title: Int values changing drastically and segmentation faults
body: <p>I've been trying to learn C++ lately, and I'm working on making a linked list of intervals that have no intersection, but i've been getting some really weird errors. My integers (obtained from get_bi or get_bs) give some pretty ridiculous values after I run the reuinion function the second time. Since those values lead to some unintended behaviour, I get segmentation faults. I thought that was the root of the problem, but I'm not sure how to fix them. I don't think I'm accessing pointers that point to nothing or whatever is causing that.</p>
  
  <pre><code>#include &lt;cstddef&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  class Uinter
      /*
       * Regrouping class for intervals (_Inter class) 
       * offering 3 different methods:
       *  "reunion(int bi, int bs)" -&gt; Unites the interval pointed by "_tete"
       *                               with the one defined by [bi, bs]
       *  "printUinter()"           -&gt; Prints the interval pointed by "_tete"
       *  "contient(int nb)"        -&gt; Returns True if "nb" is present inside
       *                               the intervals, else False. 
       */
      {
          class _Inter
          /*
           * Class representing a single interval.
           * _bi stands for the lower limit of the interval.
           * _bs stands for the upper limit of the interval.
           */
          {
              int _bi, _bs;
              _Inter* _next;
          public:
              _Inter(int _bi, int _bs, _Inter* _next)
                  {
                  this-&gt;_bi = _bi;
                  this-&gt;_bs = _bs;
                  this-&gt;_next = _next;
                  cout&lt;&lt;"_Inter created with bi, bs and _next = " &lt;&lt; _bi &lt;&lt; " , "&lt;&lt; _bs &lt;&lt; " , " &lt;&lt; _next &lt;&lt;endl;  
                  };
  
                  // Defining getters and setters for the interval.
  
              void set_bi(int value)
                  {cout &lt;&lt;"setting bi = "&lt;&lt; _bi &lt;&lt; endl;
                  _bi = value;}
  
              void set_bs(int value)
                  {cout &lt;&lt;"setting bs = "&lt;&lt; _bi &lt;&lt; endl;
                  _bs = value;}
  
              void set_next(_Inter* value)
                  {_next = value;}
  
              int get_bi()
                  {cout &lt;&lt;"getting bi = "&lt;&lt; _bi &lt;&lt; endl;
                  return _bi;}
  
              int get_bs()
                  {cout &lt;&lt;"getting bs = "&lt;&lt; _bs &lt;&lt; endl;
                  return _bs;}
  
              _Inter* get_next()
                  {cout &lt;&lt; "next = " &lt;&lt; _next &lt;&lt; endl;
                  return _next;}
  
              void print_self()
                  {
                  cout &lt;&lt; "[" &lt;&lt; this-&gt;_bi &lt;&lt; ";" &lt;&lt; this-&gt;_bs &lt;&lt; "]" &lt;&lt; endl;
                  }
          };
          _Inter* _tete;
      public:
          Uinter(): _tete(nullptr){}
          void reunion(int bi, int bs)
              {
              if(_tete == nullptr)
                  {cout &lt;&lt; "Case 1" &lt;&lt; endl;
                  _Inter new_interval = _Inter(bi, bs, nullptr);
                  _tete = &amp;new_interval;
                  cout &lt;&lt; "_tete after Case 1 end = " &lt;&lt; _tete &lt;&lt; endl;
                  cout &lt;&lt; "_tete values after Case 1 end = " &lt;&lt; _tete-&gt;get_bi() &lt;&lt; " " &lt;&lt; _tete-&gt;get_bs() &lt;&lt; " " &lt;&lt; _tete-&gt;get_next() &lt;&lt; endl;
                  cout &lt;&lt; "_tete-&gt;get_next() == nullptr = " &lt;&lt; (_tete-&gt;get_next() == nullptr) &lt;&lt; endl;
                  }
              else
                  {cout &lt;&lt; "Case 2" &lt;&lt; endl;
                  if(bi &lt; _tete-&gt;get_bi())
                      {cout &lt;&lt; "Case 2.1" &lt;&lt; endl;
                      if (bs &lt; _tete-&gt;get_bi())
                          {cout &lt;&lt; "Case 2.1.1" &lt;&lt; endl;
                          _Inter new_interval = _Inter(bi, bs, _tete);
                          _tete = &amp;new_interval;
                          }
  
                      else if (bs &gt; _tete-&gt;get_bs())
                          {cout &lt;&lt; "Case 2.1.2" &lt;&lt; endl;
                          _tete-&gt;set_bi(bi);
                          _Inter* temp;
                          temp = _tete-&gt;get_next();
                          while (temp != nullptr &amp;&amp;bs &gt; temp-&gt;get_bs())
                              {cout &lt;&lt; "Case 2.1.2 while" &lt;&lt; endl;
                              temp = temp-&gt;get_next();
                              }
                          if (temp == nullptr)
                              {cout &lt;&lt; "Case 2.1.2.1" &lt;&lt; endl;
                              _tete-&gt;set_bs(bs);
                              _tete-&gt;set_next(nullptr);
                              }
                          else
                              {cout &lt;&lt; "Case 2.1.2.2" &lt;&lt; endl;
                              _tete-&gt;set_bs(temp-&gt;get_bs());
                              _tete-&gt;set_next(temp-&gt;get_next());
                              }
                          }
  
                      else if (bs &gt; _tete-&gt;get_bi())
                          {cout &lt;&lt; "Case 2.1.3" &lt;&lt; endl;
                          _tete-&gt;set_bi(bi);
                          }                   
                      }
                  else if (bi &gt; _tete-&gt;get_bi())
                      {cout &lt;&lt; "Case 2.2" &lt;&lt; endl;
                      if (bi &gt; _tete-&gt;get_bs())
                          {cout &lt;&lt; "Case 2.2.1" &lt;&lt; endl;
                          _Inter* temp;
                          temp = _tete-&gt;get_next();
  
                          while(temp != nullptr &amp;&amp; bi &gt; temp-&gt;get_bs())
                              {cout &lt;&lt; "Case 2.2.1 while" &lt;&lt; endl;
                              temp = temp-&gt;get_next();
                              }
                          if (temp == nullptr)
                              {cout &lt;&lt; "Case 2.2.1.1" &lt;&lt; endl;
                              _Inter new_interval = _Inter(bi, bs, nullptr);
                              temp-&gt;set_next(&amp;new_interval);
                              }
                          else if (bi &lt; temp-&gt;get_bi())
                              {cout &lt;&lt; "Case 2.2.1.2" &lt;&lt; endl;
                              if (bs &lt; temp-&gt;get_bi())
                                  {cout &lt;&lt; "Case 2.2.1.2.1" &lt;&lt; endl;
                                  _Inter new_interval = _Inter(temp-&gt;get_bi(), temp-&gt;get_bs(), temp-&gt;get_next());
                                  temp-&gt;set_bi(bi);
                                  temp-&gt;set_bs(bs);
                                  temp-&gt;set_next(&amp;new_interval);
                                  }
  
                              else if (bs &gt; temp-&gt;get_bs())
                                  {cout &lt;&lt; "Case 2.2.1.2.2" &lt;&lt; endl;
                                  temp-&gt;set_bi(bi);
                                  temp-&gt;set_bs(bs);
                                  }
  
                              else if (bs &gt; temp-&gt;get_bi())
                                  {cout &lt;&lt; "Case 2.2.1.2.3" &lt;&lt; endl;
                                  temp-&gt;set_bi(bi);
                                  }                   
                              }
                          else if (bi &gt; temp-&gt;get_bi())  // bi &lt; temp.get_bs()
                              {cout &lt;&lt; "Case 2.2.1.3" &lt;&lt; endl;
                              if (bs &gt; temp-&gt;get_bs())
                                  {cout &lt;&lt; "Case 2.2.1.2.1" &lt;&lt; endl;
                                  temp-&gt;set_bs(bs);
                                  }
                              }
                          }
                      else if (bi &lt; _tete-&gt;get_bs())
                          {// bi &gt; tete-&gt;get_bi() et bi &lt; _tete-&gt;get_bs()
                          cout &lt;&lt; "Case 2.2.2" &lt;&lt; endl;
                          _Inter* temp;
                          temp = _tete;
                          while (temp != nullptr &amp;&amp; bs &gt; temp-&gt;get_bs())
                              {cout &lt;&lt; "Case 2.2.2 while" &lt;&lt; endl;
                              temp = temp-&gt;get_next();    
                              }
                          if (temp == nullptr)
                              {cout &lt;&lt; "Case 2.2.2.1" &lt;&lt; endl;
                              _tete-&gt;set_bs(bs);
                              _tete-&gt;set_next(nullptr);
                              }
                          else
                              {// bi &gt; tete-&gt;get_bi() et bi &lt; _tete-&gt;get_bs() et bs &lt; temp-&gt;get_bs()
                              cout &lt;&lt; "Case 2.2.2.2" &lt;&lt; endl;
                              if(bs &gt; temp-&gt;get_bi())
                                  {cout &lt;&lt; "Case 2.2.2.2.1" &lt;&lt; endl;
                                  _tete-&gt;set_bs(temp-&gt;get_bs());
                                  _tete-&gt;set_next(temp-&gt;get_next());
                                  }
                              else
                                  {cout &lt;&lt; "Case 2.2.2.2.2" &lt;&lt; endl;
                                  _tete-&gt;set_bs(bs);
                                  _tete-&gt;set_next(temp);
                                  }
                              }
                          }
                      }
                  }
              }
          void printUinter()
              {
              }
  
          bool contient(int nb);
      };
  
  
  int main() 
  {
      Uinter interval;
      interval.reunion(12, 23);
      interval.printUinter();
      cout &lt;&lt; "___________" &lt;&lt; endl;
      interval.reunion(24,36);
      interval.printUinter();
      cout &lt;&lt; "___________" &lt;&lt; endl;
      interval.reunion(0,11);
      interval.printUinter();
      cout &lt;&lt; "___________" &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  
  <p>Here is the result I get when I run my compiler (I don't really know what it represents, since it just says "Run failed"). </p>
  
  <pre><code>&gt; Case 1
  &gt; _Inter created with bi, bs and _next = 12 , 23 , 0
  &gt; _tete after Case 1 end = 0x23ca90 next = 0 getting bs = 23 getting bi = 12
  &gt; _tete values after Case 1 end = 12 23 0 next = 0
  &gt; _tete-&gt;get_next() == nullptr = 1
  &gt; ___________ Case 2 getting bi = -17000128 getting bi = -17000128 Case 2.2 getting bs = 3 Case 2.2.1 next = 0x1fefcb6c0
  &gt; 
  &gt; RUN FAILED (exit value 1, total time: 274ms)
  </code></pre>
  
  <p>I've read that running a debugger might help, but running the debugger in NetBeans 8.0.2 (gdb) only gives me a segmentation error and no real information on it, so I'm not really sure how to fix it. It highlights a line:</p>
  
  <pre><code>    int get_bs()
          {cout &lt;&lt;"getting bs = "&lt;&lt; _bs &lt;&lt; endl;
          return _bs;}
  </code></pre>
  
  <p>But I'm not really sure how I'm suppose to interpret that, since there are no pointers or whatever on that line.</p>
  
  <p><img src="https://i.stack.imgur.com/8LjwF.png" alt="Here is what I get on my debugger"></p>