title: Heap Block Error While Reading File Contents
body: <p>I am attempting to write a program which reads the letters "A, B, C, D, E" from a file, notes all instances of these characters, and ignores any characters in between the greater than and less than signs,  "&lt; and >". Although the file seems to have been opened correctly, the for loop located in findCommonAnswers() seems to access values that are protected or non-existent. Windows provides the exception code c0000005 and the GNU debugger tells me that "A Heap block at 0xXXXXX has been modified at 0xXXXXX+1 past request size of 6". The number 6 has been constant in each debugging. The current output of the code is "-1.#IND", in previous versions it returned a zero for all items except the constant "probabilityArrayLength". If anyone could give me any insight of the nature of the error, it would be much appreciated. Here is my code:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  ...
  ...
  int fsize(FILE *fp)
  {
  
      int prev = ftell(fp);
      fseek(fp, 0L, SEEK_END);
      int sz = ftell(fp);
      fseek(fp,prev,SEEK_SET);    
    return sz;
  }
  
  double* findCommonAnswerChoices( )
  {
    FILE *data;  
    data = fopen( "F:\\COMP_SCI\\APCalculusBCMultipleChoiceResults.txt", "r" );
  
  int bufferSize = fsize( data ), totalAnswers = 0, instanceOfA = 0, instanceOfB = 0,
  instanceOfC = 0, instanceOfD = 0, instanceOfE = 0, probabilityArrayLength =6,    bytesRead = 0;
  int j = 0 , k;
  
  double probOfA = 0.0, probOfB = 0.0, probOfC = 0.0, probOfD = 0.0, probOfE = 0.0;
  double* probabilities = (double*)malloc( probabilityArrayLength*(sizeof(double)) );
  
  char answers[bufferSize];
  
  if( data==NULL )
  {
      printf( "%d %s %d \n",bufferSize, " ", sizeof(data) );
      printf( "Not Enough Memory" );
  
  }
  else
  {
      bytesRead = fread( answers, 0, bufferSize, data );
  }
  
  
  for( j = 0; j&lt;bytesRead; j++ )
  {
      if( answers[j]=='&lt;' )
      {
          do
          {
              j++;
          }
          while( answers[j]!='&gt;' );
      }
  
      switch( answers[j] )
      {
  
          case 'A':
              instanceOfA++;
              break;
  
          case 'B':
              instanceOfB++;
              break;
  
          case 'C':
              instanceOfC++;
              break;
  
          case 'D':
              instanceOfD++;
              break;
  
          case 'E':
              instanceOfE++;
              break;
  
          default:
              break;
      }
  
  }
  
  fclose(data);
  
  totalAnswers = ( instanceOfA+instanceOfB+instanceOfC+instanceOfD+instanceOfE );
  probOfA = ( (double)instanceOfA/(double)totalAnswers );
  probOfB = ( (double)instanceOfB/(double)totalAnswers );
  probOfC = ( (double)instanceOfC/(double)totalAnswers );
  probOfD = ( (double)instanceOfD/(double)totalAnswers );
  probOfE = ( (double)instanceOfE/(double)totalAnswers );
  
  probabilities[0] = (double)probabilityArrayLength;
  probabilities[1] = probOfA;
  probabilities[2] = probOfB;
  probabilities[3] = probOfC;
  probabilities[4] = probOfD;
  probabilities[5] = probOfE;
  
  return probabilities;
  }
  
   int main()
   {
  double* something = findCommonAnswerChoices();
  int size = (int)something[0];
  int p;
  
  for( p = 0; p&lt;size; p++  )
  {
      printf( "%g \n", something[p] );
  }
  free( something );
  return 0;
   }
  </code></pre>