title: Program received signal SIGABRT, Aborted
body: <p>There is a structure in my program</p>
  
  <pre><code>struct List      
  {
      int data;
      List *next;
  };
  </code></pre>
  
  <p>and a function of adding an element to the tail of the list:</p>
  
  <pre><code>void addL(List* &amp;tail, int dat)   
  {
  
      if (tail==NULL) 
      {
          tail = new List;
          tail-&gt;data = dat; 
          tail-&gt;next=NULL;
      }   
      else
      {
          tail-&gt;next = new List;
          tail = tail-&gt;next;
          tail-&gt;data = dat;
          tail-&gt;next = NULL;
      }
  }
  </code></pre>
  
  <p>gdb says about the problem</p>
  
  <pre><code>terminate called after throwing an instance of 'St9bad_alloc'
    what():  std::bad_alloc
  
  Program received signal SIGABRT, Aborted.
  0xb7fdd424 in __kernel_vsyscall ()
  </code></pre>
  
  <p>in line </p>
  
  <pre><code>tail-&gt;next = new List;
  </code></pre>
  
  <p>I tried to make another variable of type List like that:</p>
  
  <pre><code>List* add;
  add = new List;
  </code></pre>
  
  <p>but got the same problem in second line.</p>
  
  <p>How to rewrite this correctly? And is it any need to paste here the function which calls addL?
  Sorry, if this question had already been asked, I could not understand while looking through them.</p>