title: When linked list element is created using calloc, how to delete element in the delete function without causing leaking issue?
body: <p>My delete function can pass the test 1-5 shown in main() but can't pass test 6. The expected result is merely &quot;abc&quot;. My result is not just &quot;abc&quot; but also with leaksing issue. Please refer to the attached image.</p>
  <p>I think it is because for test 6 the node for 'g' is created explicitly using calloc which needs to be freed using free() method explicitly since the delete function I show here only delete and free memory of the corresponding node from the linkedlist. It doesn't interfere with the element passed. I try to rewrite the code many times but still can't solve the leaking issue.</p>
  <p>Any help would be appreciated!</p>
  <p><a href="https://i.stack.imgur.com/Z1R0O.png" rel="nofollow noreferrer">enter image description here</a></p>
  <pre><code>#ifndef LINKEDLIST_H
  #define LINKEDLIST_H
  
  #include &lt;stdint.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct linkedlistelement_s {
      char data;
      struct linkedlistelement_s* next;
  };
  
  typedef struct linkedlistelement_s LinkedListElement_t;
  
  typedef struct {
      LinkedListElement_t* head;
      LinkedListElement_t* tail;
  } LinkedList_t;
  
  void addElement(LinkedList_t* list, char data);
  LinkedListElement_t* findElement(LinkedList_t* list, char data);
  void deleteElement(LinkedList_t* list, LinkedListElement_t* element);
  
  int main()
  {
    /* MAKE SURE TO CHECK THE LEAK USING BELOW COMMAND LINE IN LERMINAL
    cc -Wall -g -Werror filename.c -lmcheck
    valgrind ./filename.out */
  
    //TEST 1 - expected: ac
    // LinkedList_t alist = {.head = NULL, .tail = NULL};
    // LinkedListElement_t* element;
    // addElement(&amp;alist, 'a');
    // addElement(&amp;alist, 'b');
    // addElement(&amp;alist, 'c');
    // element = findElement(&amp;alist, 'b');
    // deleteElement(&amp;alist, element);
    // for (LinkedListElement_t* elem=alist.head; elem != NULL; elem = elem-&gt;next) {
    //     printf(&quot;%c&quot;, elem-&gt;data);
    // }
    // //Clean up
    // while (alist.head != NULL) {
    //     deleteElement(&amp;alist, alist.head);
    // }
  
  
    // //TEST 2 - expected: correct
    // LinkedList_t alist = {.head = NULL, .tail = NULL};
    // LinkedListElement_t* element;
    // addElement(&amp;alist, 'a');
    // element = findElement(&amp;alist, 'a');
    // deleteElement(&amp;alist, element);
    // printf(&quot;%s&quot;, (alist.head == NULL &amp;&amp; alist.tail == NULL) ? &quot;Correct&quot; : &quot;Error&quot;);
    // //Clean up
    // while (alist.head != NULL) {
    //     deleteElement(&amp;alist, alist.head);
    // }
  
    //
    // //TEST 3 - expected: abc
    // LinkedList_t alist = {.head = NULL, .tail = NULL};
    // LinkedListElement_t* element;
    // addElement(&amp;alist, 'a');
    // addElement(&amp;alist, 'b');
    // addElement(&amp;alist, 'c');
    // element = findElement(&amp;alist, 'g');
    // deleteElement(&amp;alist, element);
    // for (LinkedListElement_t* elem=alist.head; elem != NULL; elem = elem-&gt;next) {
    //     printf(&quot;%c&quot;, elem-&gt;data);
    // }
    // //Clean up
    // while (alist.head != NULL) {
    //     deleteElement(&amp;alist, alist.head);
    // }
  
    //TEST 4 - expected: a
    // LinkedList_t alist = {.head = NULL, .tail = NULL};
    // LinkedListElement_t* element;
    // addElement(&amp;alist, 'a');
    // addElement(&amp;alist, 'b');
    // element = findElement(&amp;alist, 'b');
    // deleteElement(&amp;alist, element);
    // for (LinkedListElement_t* elem=alist.head; elem != NULL; elem = elem-&gt;next) {
    //     printf(&quot;%c&quot;, elem-&gt;data);
    // }
    // //Clean up
    // while (alist.head != NULL) {
    //     deleteElement(&amp;alist, alist.head);
    // }
    //
    // //TEST 5 - expected: b
    // LinkedList_t alist = {.head = NULL, .tail = NULL};
    // LinkedListElement_t* element;
    // addElement(&amp;alist, 'a');
    // addElement(&amp;alist, 'b');
    // element = findElement(&amp;alist, 'a');
    // deleteElement(&amp;alist, element);
    // for (LinkedListElement_t* elem=alist.head; elem != NULL; elem = elem-&gt;next) {
    //     printf(&quot;%c&quot;, elem-&gt;data);
    // }
    // //Clean up
    // while (alist.head != NULL) {
    //     deleteElement(&amp;alist, alist.head);
    // }
  
    // // //TEST 6 - expected: abc
    // LinkedList_t alist = {.head = NULL, .tail = NULL};
    // LinkedListElement_t* element;
    // addElement(&amp;alist, 'a');
    // addElement(&amp;alist, 'b');
    // addElement(&amp;alist, 'c');
    // element = (LinkedListElement_t*)calloc(sizeof(LinkedListElement_t), 1);
    // element-&gt;data = 'g';
    // deleteElement(&amp;alist, element);
    // for (LinkedListElement_t* elem=alist.head; elem != NULL; elem = elem-&gt;next) {
    //     printf(&quot;%c&quot;, elem-&gt;data);
    // }
    // //Clean up
    // while (alist.head != NULL) {
    //     deleteElement(&amp;alist, alist.head);
    // }
  }
  
  void addElement(LinkedList_t* list, char data)
  {
      LinkedListElement_t* element = calloc(1, sizeof(LinkedListElement_t));
      element-&gt;data = data;
      element-&gt;next = NULL;
  
      if (list-&gt;tail == NULL) {
          list-&gt;head = element;
          list-&gt;tail = element;
      } else {
          list-&gt;tail-&gt;next = element;
          list-&gt;tail = element;
          element-&gt;next = NULL;
      }
  }
  
  LinkedListElement_t* findElement(LinkedList_t* list, char data)
  {
      LinkedListElement_t* element = list-&gt;head;
      while (element != NULL) {
        if (element-&gt;data == data) {
          return element;
        }
        element = element-&gt;next;
      }
      return NULL;
  }
  
  
  void deleteElement(LinkedList_t *list, LinkedListElement_t *element)
  {
      // No Element
      if (element == NULL) {
          return;
      }
  
      // list is empty
      if (list-&gt;head == NULL) {
          return;
      }
  
      if (list-&gt;head-&gt;data == element-&gt;data) {
          LinkedListElement_t * temp = list-&gt;head;
          list-&gt;head = list-&gt;head-&gt;next;
          free(temp);
  
          if (list-&gt;head == NULL) {
              list-&gt;tail = NULL;
          }
      } else {
          LinkedListElement_t *curr = list-&gt;head-&gt;next;
          LinkedListElement_t *prev = list-&gt;head;
  
          while (curr != NULL &amp;&amp; curr-&gt;data != element-&gt;data) {
              prev = curr;
              curr = curr-&gt;next;
          }
  
          // element not found
          if (curr == NULL) {
              return;
          }
  
          if (curr-&gt;data == element-&gt;data) {
              prev-&gt;next = curr-&gt;next;
              free(curr);
          }
      }
  }
  
  #endif
  </code></pre>