title: C - How can I free dynamically allocated memory?
body: <p>Have a look at this piece of codes, it's part of a linked list.</p>
  
  <pre><code>int main()
  
  {
      List* head1 = NULL;
  
      insertFront(&amp;head1, 1);
      insertFront(&amp;head1, 2);
  
      print(head1);
  
      free(head1);
  
      return 0;
  }
  </code></pre>
  
  <p>another function is:</p>
  
  <pre><code>void insertFront(List** head, int value)
  
  {
      List* node = (List*)malloc(sizeof(List));
      node-&gt;data = value;
      node-&gt;next = NULL;
  
      node-&gt;next = *head;
      *head = node;
  
     //free(node); essentially I am not freeing node
  }
  </code></pre>
  
  <p>My questions are:</p>
  
  <ol>
  <li><p>Is my code going to cause memory leak problem?</p></li>
  <li><p>Should I need to free the allocated memory (dynamically) for node (Which is inside a function)?</p></li>
  <li><p>If I free head1, will the memory allocated for node also be freed? If yes, then how?</p></li>
  </ol>