title: Why is this segmentation fault inconsistent across builds?
body: <p>I wrote a c program, compiled it and it ran fine. After a few compiles - it started giving me a segmentation fault. I renamed the folder, recompiled and it worked again.<br>
  Is this something normal? To have an inconsistent segmentation fault? I change the output name, change folder names etc.. and it bounces from giving segmentation fault to not giving seg fault. I don't know what to do anymore.<br>
  I mean, if it is a coding problem, seg fault should be consistent, right? I should get it every time. here's the code:<br>
  file my_set.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include "list.h"
  
  /*
  The program acceps a set of numbers from stdin until EOF
  And then prints them (not storing duplicate numbers)
  */
  
  int main ()
  {
      int num; 
      nodePtr head; /*head of the list*/
  
      while (scanf("%d", &amp;num) != EOF)
      {
          addToList(num, &amp;head);
      }
      printList(head);
      freeList(head);
      return 0;
  }
  </code></pre>
  
  <p>file list.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include "list.h"
  
  /*
  Implements a linked list, each element of which contains a dynamic array.
  I used a linked list to maximize potential memory in case it is fragmented.
  I use a dynamic array in each node to minimize the percentage of overhead
  from creating a list (the pointer, the index...);
  */
  
  /*
  Adds number n to list *h
  4 cases:
  1. list is empty:
      creating one
      updating h with new list
      creating a new dynamic array in the list
      updating it and the index
  2. can reallocate current node's array for 1 more int
  3. cannot reallocate current node's array:
      creating a new node
      initializing it
  4. cannot create a new node
      printing the current list, an "out of memory error" and freeing all memory.
  */
  void addToList(int n, nodePtr *h)
  {
      static nodePtr p; /*points to current last node*/
      int *temp; /*for use in reallocation*/
  
      if (!*h) /*first item of set*/
      {
          *h = malloc (sizeof(node));
          (*h)-&gt;arr = malloc(sizeof(int));
          (*h)-&gt;arr[0] = n;
          (*h)-&gt;i = 1;
          p = *h;
          return;
      }
  
      /*if n is already in the list, no need to add it
      the call comes after first item, because first item cannot be in the list*/
      if(existsInList(n, *h)) return;
  
      /*using realloc while still possible*/
      if ((temp = realloc(p-&gt;arr, (p-&gt;i+1)*sizeof(int))))
      {
          p-&gt;arr = temp;
          p-&gt;arr[p-&gt;i] = n;
          p-&gt;i++;
          return;
      }
  
      /*if  realloc no longet possible - start new node*/
      if ((p-&gt;next = malloc(sizeof(node))))
      {
          p = p-&gt;next;
          p-&gt;arr = malloc(sizeof(int));
          p-&gt;arr[0] = n;
          p-&gt;i = 1;
          return;
      }
  
      /*can no longer start new nodes - quit with error, after printing*/
      printf("out of memory!");
      printList(*h);
      freeList(*h);
  }
  
  /*checks if n is in p assuming p is not null
  it can asume so because the call for it comes after the check for first item*/
  int existsInList(int n, nodePtr p)
  {
      int i;
      for (; p ; p = p-&gt;next)
          for (i = 0; i &lt; p-&gt;i; i++)
              if (p-&gt;arr[i] == n)
                  return 1;
      return 0;
  }
  
  /*frees the list*/
  void freeList(nodePtr p)
  {
      nodePtr temp = p;
  
      if (!p) return; /*list is empty*/
  
      while (p)
      {
          free(p-&gt;arr);
          p = p-&gt;next;
          free(temp);
      }
  }
  
  /*prints the content of the list to stdout*/
  void printList(nodePtr p)
  {
      if (!p) return;
      int i;
      printf("\n");
      for (; p ; p = p-&gt;next)
          for (i = 0; i &lt; p-&gt;i; i++)
              printf("%d ", p-&gt;arr[i]);   
      printf("\n");
  }
  </code></pre>
  
  <p>file list.h:</p>
  
  <pre><code>/*
  pointer to a node
  declare a variable of this type to create a list
  then start adding to the list
  */
  typedef struct s *nodePtr;
  
  /*the struct that represents each node of the list
  reason for dynamic array is in "list.c"
  */
  typedef struct s
  {
      int *arr;
      int i; /*index for next num, also size of array;*/
      nodePtr next;
  }node;
  
  /*Adds the int to list at nodePtr omitting duplicates*/
  void addToList(int, nodePtr*);
  /*prints a list*/
  void printList(nodePtr);
  /*returns 1 if an int exists in list referenced by nodePtr, 0 otherwise*/
  int existsInList(int, nodePtr);
  /*frees all dynamically allocated memory*/
  void freeList(nodePtr);
  </code></pre>
  
  <p>Basically all I do is get numbers from stdin, put them in a list(no duplicates) and then print them. I use a list of dynamic arrays.</p>