title: Program Segmentation Faults on return 0/fclose/free. I think I have memory leaks but can't find them. Please help!
body: <p>I am trying to write a Huffman encoding program to compress a text file. Upon completetion, the program will terminate at the return statement, or when I attempt to close a file I was reading from. I assume I have memory leaks, but I cannot find them. If you can spot them, let me know (and a method for fixing them would be appreciated!).</p>
  
  <p>(note: small1.txt is any standard text file)</p>
  
  <p>Here is the main program</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  #include&lt;stdlib.h&gt;
  #define ASCII 255
  
  struct link {
   int freq;
   char ch[ASCII];
   struct link* right;
   struct link* left;
  };
  
  typedef struct link node;
  typedef char * string;
  FILE * ofp;
  FILE * ifp;
  int writebit(unsigned char);
  void sort(node *[], int);
  node* create(char[], int);
  void sright(node *[], int);
  void Assign_Code(node*, int[], int, string *);
  void Delete_Tree(node *);
  
  int main(int argc, char *argv[]) {
   //Hard-coded variables
   //Counters
  
   int a, b, c = 0;
   //Arrays
   char *key = (char*) malloc(ASCII * sizeof(char*));
   int *value = (int*) malloc(ASCII * sizeof(int*));
  
   //File pointers
  
   FILE *fp = fopen(argv[1], "r");
   if (fp == NULL) {
    fprintf(stderr, "can't open %s\n", argv[1]);
    return 0;
   }
   //Nodes
   node* ptr;//, *head;
   node* array[ASCII];
  
   //
   int u, carray[ASCII];
   char str[ASCII];
  
   //Variables
   char car = 0;
   int inList = 0;
   int placeinList = -1;
   int numofKeys;
  
   if (argc &lt; 2) {
    printf("Usage: huff &lt;.txt file&gt; \n");
    return 0;
   }
  
   for (a = 0; a &lt; ASCII; a++) {
    key[a] = -1;
    value[a] = 0;
   }
  
   car = fgetc(fp);
   while (!feof(fp)) {
    for (a = 0; a &lt; ASCII; a++) {
     if (key[a] == car) {
      inList = 1;
      placeinList = a;
     }
    }
    if (inList) {
     //increment value array
     value[placeinList]++;
     inList = 0;
    } else {
     for (b = 0; b &lt; ASCII; b++) {
      if (key[b] == -1) {
       key[b] = car;
       break;
      }
     }
    }
    car = fgetc(fp);
   }
   fclose(fp);
   c = 0;
   for (a = 0; a &lt; ASCII; a++) {
    if (key[a] != -1) {
     array[c] = create(&amp;key[a], value[a]);
     numofKeys = c;
     c++;
    }
   }
  
   string code_string[numofKeys];
  
   while (numofKeys &gt; 1) {
    sort(array, numofKeys);
    u = array[0]-&gt;freq + array[1]-&gt;freq;
    strcpy(str, array[0]-&gt;ch);
    strcat(str, array[1]-&gt;ch);
    ptr = create(str, u);
    ptr-&gt;right = array[1];
    ptr-&gt;left = array[0];
    array[0] = ptr;
    sright(array, numofKeys);
    numofKeys--;
   }
  
   Assign_Code(array[0], carray, 0, code_string);
  
   ofp = fopen("small1.txt.huff", "w");
  
   ifp = fopen("small1.txt", "r");
  
   car = fgetc(ifp);
   while (!feof(ifp)) {
    for (a = 0; a &lt; ASCII; a++) {
     if (key[a] == car) {
      for (b = 0; b &lt; strlen(code_string[a]); b++) {
       if (code_string[a][b] == 48) {
        writebit(0);
       } else if (code_string[a][b] == 49) {
        writebit(1);
       }
      }
     }
    }
    car = fgetc(ifp);
   }
   writebit(255);
   fclose(ofp);
   ifp = fopen("small1.txt", "r");
   fclose(ifp);
   free(key);
   //free(value);
   //free(code_string);
   printf("here1\n");
   return 0;
  }
  
  int writebit(unsigned char bitval) {
  
   static unsigned char bitstogo = 8;
   static unsigned char x = 0;
  
   if ((bitval == 0) || (bitval == 1)) {
    if (bitstogo == 0) {
     fputc(x, ofp);
     x = 0;
     bitstogo = 8;
    }
    x = (x &lt;&lt; 1) | bitval;
    bitstogo--;
   } else {
    x = (x &lt;&lt; bitstogo);
    fputc(x, ofp);
   }
  
   return 0;
  }
  void Assign_Code(node* tree, int c[], int n, string * s) {
   int i;
   static int cnt = 0;
   string buf = malloc(ASCII);
   if ((tree-&gt;left == NULL) &amp;&amp; (tree-&gt;right == NULL)) {
    for (i = 0; i &lt; n; i++) {
     sprintf(buf, "%s%d", buf, c[i]);
    }
    s[cnt] = buf;
    cnt++;
   } else {
    c[n] = 1;
    n++;
    Assign_Code(tree-&gt;left, c, n, s);
    c[n - 1] = 0;
    Assign_Code(tree-&gt;right, c, n, s);
   }
  }
  node* create(char a[], int x) {
   node* ptr;
   ptr = (node *) malloc(sizeof(node));
   ptr-&gt;freq = x;
   strcpy(ptr-&gt;ch, a);
   ptr-&gt;right = ptr-&gt;left = NULL;
   return (ptr);
  }
  
  void sort(node* a[], int n) {
   int i, j;
   node* temp;
   for (i = 0; i &lt; n - 1; i++)
    for (j = i; j &lt; n; j++)
     if (a[i]-&gt;freq &gt; a[j]-&gt;freq) {
      temp = a[i];
      a[i] = a[j];
      a[j] = temp;
     }
  }
  
  void sright(node* a[], int n) {
   int i;
   for (i = 1; i &lt; n - 1; i++)
    a[i] = a[i + 1];
  }
  </code></pre>