title: c++ pointer being freed was not allocated error when deleting pointer
body: <p>I am new to c++ , I want to remove the maximum value in linked list. But I got the error message. Here is the function that removing the max value and return the value removed. While running the line "delete cur", I got the error message. Somebody help me please! </p>
  
  <pre><code>int removeMax(Dnode*&amp; list) {
  Dnode *p = list-&gt;next;
  Dnode *cur = list;
  int max = list-&gt;info;
  
  while (p != NULL) {
      if (p-&gt;info &gt; max) {
          max = p-&gt;info;
          cur = p;
      }
      p = p-&gt;next;
  }
  
  if(cur-&gt;back == NULL &amp;&amp; cur != NULL){
      Dnode *after = cur-&gt;next;
      after-&gt;back = NULL;
      delete cur;
  }
  else if(cur-&gt;next == NULL &amp;&amp; cur != NULL){
      Dnode *pre = cur-&gt;back;
      pre-&gt;next = NULL;
      delete cur;
  }
  else{
      Dnode *pre = cur-&gt;back;
      Dnode *after = cur-&gt;next;
      pre-&gt;next = cur-&gt;back;
      after-&gt;back = pre;
      delete cur;
  }
  return max;
  </code></pre>
  
  <p>}</p>