title: I'm trying to read a file into an arrray and keep getting this error
body: <p>help me please. I'm trying to read a file into an array and return the max amount of numbers in the file. I keep getting a segmentation fault error and I know its probably because of how I'm handling the Array but I can find the exact issue, The issue is in the function ReadFileInotArray. After the function is over I get the error shown below.</p>
  <pre><code>#include &lt;stdlib.h&gt; 
  #include &lt;stdio.h&gt; 
  #include &lt;string.h&gt;
  #include &lt;time.h&gt;
  void PrintArray(int ArrayToPrint[], int SizeAP)
  {
      int i;
      printf(&quot;{&quot;);
      for (i=0; i&lt; SizeAP; i++)
          printf(&quot;%d,&quot;, ArrayToPrint[i]);
      printf(&quot;\b}\n&quot;);
  }
  
  void swap(int *SwapA, int *SwapB)
  {
      int temp = *SwapA;
      *SwapA = *SwapB;
      *SwapB = temp;
  }
  
  int partition (int A[], int low, int high) 
  { 
      int i, j = 0;
      #if QSM
      int middle = (high+low)/2;
      swap(&amp;A[middle], &amp;A[high]);
      #elif QSRND
      int random = (rand() % (high-low+1)) + low;
      swap(&amp;A[random], &amp;A[high]);
      #endif
      int pivot = A[high];
      i= (low - 1); 
      for (j = low; j &lt; high; j++) 
      { 
          if (A[j] &lt; pivot) 
          { 
              i++; 
              swap(&amp;A[i], &amp;A[j]);
          } 
      } 
      swap(&amp;A[i+ 1], &amp;A[high]);
      return (i+ 1);
  }
  
  void QuickSort(int A[], int low, int high) 
  { 
      if (low &lt; high) 
      {
         int ndx= partition(A, low, high);
         QuickSort(A, low, ndx-1); 
         QuickSort(A, ndx+ 1, high); 
      } 
  }
  
  void ReadFileIntoArray(int argc, char *argv[], int A[],int size)
  {
      char line[200];
      int j=0;
      if(argv[1]==NULL) 
      {   
          printf(&quot;File must be provided on command line...exiting\n&quot;);
          exit(0);
      }
      
      FILE *fp = fopen(argv[1],&quot;r&quot;);
      
      if(fp==NULL) 
      {
          printf(&quot;Exiting: no such file in directory\n&quot;);
          exit(0);
      }
      while(fgets(line,200,fp)!=NULL) 
      {
          size++;
      }
      fseek(fp,0,0); 
      A=malloc(sizeof(int)*size);
      while(j&lt;size &amp;&amp; fgets(line,200,fp)!=NULL ) 
      {
      
          A[j]=atoi(line);
          printf(&quot;%d\n&quot;,A[j]);
          j++;
      }
      printf(&quot;%d&quot;,size);
      PrintArray(A,size);
  
  }
  
  int main(int argc ,char *argv[]) 
  { 
      clock_t start,end;
      int i,control,count=0,size;
      int *array;
      
      if(argv[2]==NULL)
      { 
          printf(&quot;Number of runs not specified on the command line...defaluting to 10&quot;);
          control=10;
      }
      else 
          control=atoi(argv[2]);
      for(i=1; i&lt;=control; i++)
      {
          ReadFileIntoArray(argc,argv,array,size);
          #ifdef PRINTARRAY
          PrintArray(array, size);
          #endif
          printf(&quot;ok&quot;);
          start = clock();
          QuickSort(array, 0, size-1);
          end = clock();
          printf(&quot;\nRun %d complete: %ld tics&quot;,i ,end-start);
          count+=end-start;
      
          #ifdef PRINTARRAY
          PrintArray(array,size);
          #endif
      
          free(array);
      }
      printf(&quot;\nThe average run time for %s runs is %d\n&quot;,argv[2],count/i);
      printf(&quot;\nProcessed %d records\n&quot;,size);
      return 0; 
  } 
  </code></pre>
  <p>Here is the output I get when running in the VM.</p>
  <pre><code>student@maverick:/media/sf_VM_folder/CSE_3318_code@ gcc Code3_1001472179.c 
  student@maverick:/media/sf_VM_folder/CSE_3318_code$ ./a.out TestFile.txt 5
  472179
  93424
  313939
  263358
  154966
  93659
  221593
  284590
  439245
  154572
  10{472179,93424,313939,263358,154966,93659,221593,284590,439245,154572}
  ok
  munmap_chunk(): invalid pointer
  Aborted (core dumped)
  </code></pre>