title: Program received signal SIGSEGV, Segmentation fault. C++ LIST
body: <p><strong>L.insert()</strong> gives segmentation fault if I call it after <strong>L.deleteElement()</strong>.
  This is the error message:</p>
  
  <pre><code>196     nPtr = (nodePtr)malloc(sizeof(node));
  (gdb) print(c)
  $7 = 2
  (gdb) next
  197     if(nPtr!=NULL){
  (gdb) print(nPtr)
  $8 = (nodePtr) 0x615c70
  (gdb) next
  198         nPtr-&gt;data = element;
  (gdb) print(element)
  $9 = "("
  (gdb) next
  
  Program received signal SIGSEGV, Segmentation fault.
  0x00007ffff7b74413 in std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::_M_assign(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) ()
     from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
  </code></pre>
  
  <p>I created a new node and didn't dereference any uninitialized pointer! Can anyone explain why I get this error? And it works as expected until I call it after L.deleteEle().</p>
  
  <p>Here is my full prog:</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;string&gt;
  using namespace std;
  struct node
  {
      string data;
      struct node *next;
  };
  typedef struct node node;
  typedef node* nodePtr;
  class List
  {
      nodePtr head;
      int count;
      nodePtr tail;
  public:
      List(nodePtr h=NULL,int c=0,nodePtr t=NULL){ // constructor
          head=h;
          count=c;
          tail=t;
      } 
      bool insert(string element, int position=1); // if position is invalid, returns false ; takes default position as START (1)
      bool deleteEle(int position); // deletes the node at specified position, else returns false.
  };
  
  bool List::deleteEle(int position)
  {
      if(position&gt;count){
          return false;
      }
      nodePtr pPtr;
      nodePtr cPtr;
      nodePtr tPtr;
      int p=position;
      if(position==1){
          cPtr=head;
          tPtr=cPtr-&gt;next;
          head=tPtr;
          free(cPtr);
          count-=1;
          return true;
      }
      cPtr=head;
      pPtr=NULL;
      while(p&gt;1){
          pPtr=cPtr;
          cPtr=cPtr-&gt;next;
          p--;
      }
      if(position==count){
          tail=pPtr;
          tail-&gt;next=NULL;
          free(cPtr);
          count-=1;
          return true;
      }
      tPtr=cPtr-&gt;next;
      pPtr-&gt;next=tPtr;
      free(cPtr);
      count-=1;
      return true;
  }
  bool List::insert(string element, int position)
  {
      if (position&gt;count+1){
          return false;
      }
      int c = position;
      nodePtr nPtr;
      nodePtr tPtr;
      nodePtr cPtr;
      nPtr = (nodePtr)malloc(sizeof(node));
      if(nPtr!=NULL){
          nPtr-&gt;data = element;
          nPtr-&gt;next = NULL;
  
          tPtr = NULL;
          cPtr = head;
          if(cPtr==NULL){
              head=nPtr;
              tail=nPtr;
              count+=1;
              return true;
          }
          else if(position==count+1){
              cout&lt;&lt;"ikkade !!!";
              tail-&gt;next=nPtr;
              tail=nPtr;
              count+=1;
              return true;
          }
          else if(position==1){
              head=nPtr;
              nPtr-&gt;next=cPtr;
              count+=1;
              return true;
          }
          else{
              while(cPtr!=NULL &amp;&amp; c&gt;2){
                  cPtr = cPtr-&gt;next;
                  c--; 
              }
              tPtr=cPtr-&gt;next;
              cPtr-&gt;next=nPtr;
              nPtr-&gt;next=tPtr;
              count+=1;
              return true;
          }
      }
      else{
          cout&lt;&lt;element&lt;&lt;" not inserted! No memory available.";
          return false;
      }
  }
  int main(void)
  {
      List L;
      L.insert("(",L.size()+1);
      L.insert("+",L.size()+1);
      L.deleteEle(L.size());
      L.insert("(",L.size()+1); //fails here
  return 0;       
  }
  </code></pre>
  
  <p>Insert fails after calling deleteEle!</p>