title: free(): invalid next size(normal)
body: <p>When running this code with an input .txt file containing somewhere between 200-300 integers (separated by spaces) i get an error right before the for loop with the fprintf statement. </p>
  
  <p>I am not sure if qsort is causing this error or why it occurs but any insight would be appreciated.</p>
  
  <p>(this file is run by adding the name of the input file and the output file in the command line ex: ./program input.txt output.txt</p>
  
  <p>My code: </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;string.h&gt;
  
  int cmpfunc (const void * a, const void * b)
  {
     return ( *(int*)a - *(int*)b );
  }
  
  int main(int argc, char *argv[]){
  
      if(argc != 3){
          printf("\nInvalid input\nPlease provide the input and output text file names as %s name1 name2\n", argv[0]);
      }else{
  
      printf("\nPart A: \n");
      printf("..............................................................................................................\n\n");
  
      char *fn1 = argv[1];  //variables
      char *fn2 = argv[2]; 
  
      int temp = 0;
      int counter = 0;
      int index = 0;
      int index2 = 0;
      int sort = 0;
  
  
      FILE *fp1 = fopen(fn1, "r"); //read file
      FILE  *fp2 = fopen(fn2, "w"); //write file
  
      if(fp1 == NULL){  //test if fp1 was opened
          printf("There was an error opening the input file");
      }
  
       char data[10]; //ints can only hold 10 digits
       int *integerArr;
       int *tempPointer;
  
      integerArr = malloc(10*sizeof(int));
  
       int sizeOfArrs = 10;
  
      printf("Reading in the textfile: ");
  
       while(fscanf(fp1,"%s",data) != EOF){  //reads in the file breaking on each whitespace and ends at the EOF pointer
  
    temp = strlen(data);
    if(temp &lt;=10){
      temp = atoi(data);
      integerArr[counter] = temp;
  
      printf(".");
  
      counter++;
      if(counter == sizeOfArrs -1){
  
          temp = sizeOfArrs * 2;
  
          tempPointer = realloc(integerArr, temp);
  
          if(tempPointer != NULL){
              integerArr = tempPointer;
          }
  
      }
    }else printf("\ninteger had too many digits\n");
  
  
      }
  
  
       printf(" Done\n%d Numbers were found\n",  counter);
       printf("The integers found in the %s file: \n", argv[1]);
  
          index = 0;  //reset index to 0;
      for(index;index&lt;counter;index++){  //prints the unsorted contents of the file
          printf("%d ", integerArr[index]);
      }
  
      printf("\n\nPart B\n");
      printf("..............................................................................................................\n\n");
  
  
      printf("The integers found in the %s file after sorting: \n", argv[1]);
  
       qsort(integerArr, counter, sizeof(int), cmpfunc); //best function ever (sorts the array using the cmpfunc to tell if an integer is greater than less than or equal to the next one)
  
      index = 0; //resets the index
      for(index; index &lt;counter; index++){ //prints the sorted contents of the file
          printf("%d ", integerArr[index]);
  
          fprintf(fp2,"%d ",integerArr[index]); //writes the sorted integers to the new file
      }
  
      if(fp2 == NULL){ //tests if the write worked
          printf("There was an error writing the outputfile");
  
      }
  
      printf("\n");
      close(fp1,fp2); //closes both files
      }
  return 0;
  }
  </code></pre>