title: Freeing linked list crashes when on last node
body: <p>This is a continuation of a problem I posted yesterday, which I thought was solved, but turns out another problem has been encountered with the way I iterate through the loop and its exit condition. I felt a new question thread might be more appropriate.</p>
  
  <p>The following freeAllListMembers() function seems to be working, up until the last iteration of the loop because it is trying to free "temp" but temp has already been freed, what method can I use to exit this loop and keep it from running once more, everything I have tried doesn't seem to be working.</p>
  
  <p>thanks for any insight</p>
  
  <pre><code>int main() {
  
      struct node *head = NULL;
      createList(&amp;head);
  
      //do stuff with list
  
      freeAllListMembers(&amp;head);
  
      return 0;
  }
  
  int createList(struct node **head) {
      struct node *newNode= NULL;
  
      for(int I = 0; I &lt; 100; I++) 
      {
           struct node *node = (struct node*)malloc(sizeof(struct node));
  
           node-&gt;data = someData;
  
           node-&gt;next = NULL;
  
           //if we havent created an initial start node, create it
           if (*head == NULL)
           {
               *head = node;
           }
           //otherwise, navigate to the end of the list to add a new node
           else
           {
               newNode = *head;
               while (newNode-&gt;next != NULL){
                   newNode = newNode-&gt;next;
               }
               newNode-&gt;next = node;
           }
      }
      return 0;
  }
  
  void freeAllListMembers(struct node **head){ 
      struct node *temp;
      while (*head != NULL) {
          temp = *head;
          *head = (*head)-&gt;next;
          free(temp);
      }
      return;  
  }
  </code></pre>