title: C - How do I free memory that is allocated in other functions?
body: <p>Here's my main:</p>
  
  <pre><code>int main()
  {
     int  i;
     int *a = readData(N);
     int *f = frequency(a, N, MAX);
     int c = 0;
  
     printf("%9s %9s\n", "Number", "Frequency");
     for( i = 0; i &lt;= MAX; i ++)
     {  
        c += f[i];
        printf("%9d %9d\n", i, f[i]); 
     }
  
     printf("total frequency for all words: %d\n", c);
     free(f);
     free(a);
     printf("All memory freed!\n");
  
     return(0);
  }
  </code></pre>
  
  <p>and here is the first supporting function</p>
  
  <pre><code>int * readData(int size)
  {
      int i, *array = malloc(N*sizeof(int));
      for(i =0; i&lt;N;i++)
          scanf("%d", &amp;array[i]);
  
      return array;
  }
  </code></pre>
  
  <p>and the second supporting function</p>
  
  <pre><code>int * frequency(int *input, int size, int max)
  {
      int i, x;
      int *farray = malloc(max+1*sizeof(int));    
      for(i = 0; i&lt;=max;i++)
          farray[i]=0;
  
      for(i = 0; i&lt;1099; i++)
      {   
          x = input[i];
          farray[x]++;
      }
  
      return farray;
  }
  </code></pre>
  
  <p>Everything works great! When I run it through valgrind, it tells me that all my memory was freed. But, when I run the program normally, it crashes at the end with a core dump when I free the allocated memory. Why? Thanks in advance! I'm new at C.</p>