title: Getting Abort trap 6 while coding SLL
body: <p>I'm new to c++. Trying to make a simple singly linked list program. The code is as follows. Here I have created a Node class, SLL class and functions accordingly.</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  class Node{
  
      private:
          int data;
          Node* next;
  
      friend class SLL;
  };
  
  class SLL{
      public:
          SLL();
          ~SLL();
          bool empty() const;
          const int&amp; front() const;
          void addFront(const int&amp; e);
          void removeFront();
          void printList();
      private:
          Node* head;
  };
  
  SLL::SLL()
      :head(NULL){ }
  
  SLL::~SLL(){
      while(!empty())
          removeFront();
  }
  
  bool SLL::empty() const{
      return head == NULL;
  }
  void SLL::removeFront(){
      Node* temp = head;
      head = temp-&gt;next;
      delete temp;
  }
  
  void SLL::addFront(const int&amp; e){
      Node* n = new Node;
      n-&gt;data = e;
      n-&gt;next = head;
      head = n;
  }
  
  void SLL::printList(){
      Node* temp = head;
      // int n =0 ;
      while(temp-&gt;next){
          // n++;
          cout&lt;&lt; temp-&gt;data &lt;&lt; " ";
          temp = temp-&gt;next;
      }
      delete temp;
  }
  
  int main(){
      SLL a;
      a.addFront(1); 
      a.printList();
  }
  </code></pre>
  
  <p>When I compile the program, getting the following error.</p>
  
  <pre><code>ol(36664,0x7fff8a6e0340) malloc: *** error for object 0x7fb055402820: pointer being freedwas not allocated
  *** set a breakpoint in malloc_error_break to debug
  Abort trap: 6
  </code></pre>
  
  <p>Why is this error appearing? WHat's the solution for this?</p>