title: Getting SIGSEGV segmentation fault in linked list structure
body: <p>I've made a very simple linked list node structure in C, with some generic pointer data and a pointer to the next node structure. I have a function which will take a linked list node, and delete it as well as any other node it links to. It is currently made like this:</p>
  
  <pre><code>void freeLinkedList(LinkedListNode *node)
  {
     LinkedListNode *currentNode = node;
     LinkedListNode *previousNode = NULL;
     do
     {
          free(currentNode-&gt;data);
  
          previousNode = currentNode;
          currentNode = currentNode-&gt;next;
  
          printf("Freeing node... %s\n", previousNode-&gt;name);
          free(previousNode);
          printf("freed it!\n");
      } while (currentNode != NULL);
  
      printf("Deleted node and all referencing nodes!");
  }
  </code></pre>
  
  <p>It very simply traverses from the node given in the function, and continues to delete the pointer data, point to the next node pointed to (if any), then delete memory for the previous node. This does work as predicted... but only in some cases.</p>
  
  <p>The actual LinkedList structure looks like this:</p>
  
  <pre><code>typedef struct LinkedListNode {
      void *data;
      struct LinkedListNode *next;
      char name[50];
  } LinkedListNode;
  </code></pre>
  
  <p>In the case of structures dynamically allocated like this, the function works perfectly:</p>
  
  <pre><code>LinkedListNode *myNode1 = malloc(sizeof(struct LinkedListNode));
  LinkedListNode *myNode2 = malloc(sizeof(struct LinkedListNode));
  LinkedListNode *myNode3 = malloc(sizeof(struct LinkedListNode));
  strcpy(myNode1-&gt;name, "Node1");
  myNode1-&gt;data = NULL;
  myNode1-&gt;next = myNode2;
  
  strcpy(myNode2-&gt;name, "Node2");
  myNode2-&gt;data = NULL;
  myNode2-&gt;next = myNode3;
  
  strcpy(myNode3-&gt;name, "Node3");
  myNode3-&gt;data = NULL;
  myNode3-&gt;next = NULL;
  
  freeLinkedList(myNode1); // CALLING DELETE FUNCTION HERE
  </code></pre>
  
  <p><em>But</em> if I use the function with structures allocated not on heap memory, but instead automatic stack memory like so:</p>
  
  <pre><code>LinkedListNode myNode1 = {NULL, NULL, "Node1"};
  LinkedListNode myNode2 = {NULL, NULL, "Node2"};
  LinkedListNode myNode3 = {NULL, NULL, "Node3"};
  
  myNode1.next = &amp;myNode2;
  myNode2.next = &amp;myNode3;
  
  freeLinkedList(&amp;myNode1); // CALLING DELETE FUNCTION HERE
  </code></pre>
  
  <p>I get a <em>SIGSEGV - segmentation fault</em> at this line in the function:</p>
  
  <pre><code>free(previousNode);
  </code></pre>
  
  <p>This error ONLY happens at the free function of the last node, that is, the output will say:
  <em>"Freeing node... node3</em></p>
  
  <p>Then crash.</p>
  
  <p>But the very funny thing is, so far I've only experienced it using the example above. If I say, declare one more local LinkedListNode struct like this:</p>
  
  <pre><code>LinkedListNode myNode1 = {NULL, NULL, "Node1"};
  LinkedListNode myNode2 = {NULL, NULL, "Node2"};
  LinkedListNode myNode3 = {NULL, NULL, "Node3"};
  LinkedListNode myNode4 = {NULL, NULL, "Node4"};
  
  myNode1.next = &amp;myNode2;
  myNode2.next = &amp;myNode3;
  
  freeLinkedList(&amp;myNode1);
  </code></pre>
  
  <p>The function actually works, and does everything as expected to.</p>
  
  <p>I've tried for a couple of hours now to think of why this could be, but I'm simply stumped. Does it have something to do with me attempting to free memory allocated on the stack?</p>