title: Unknown C++ error: Fatal error: glibc detected an invalid stdio handle
body: <p>I have been searching all over the internet to try and understand why I am getting this error, but I have only ever seen this appear in other programming languages I am unfamiliar with or with code I have never used before. </p>
  
  <p>I am working on a quicksort program for a school assignment, and everything was going well until I decided to consolidate the median bubble sort algorithm into a helper function. </p>
  
  <pre class="lang-cpp prettyprint-override"><code>int* QS::MedianSort(int left, int middle, int right) {
      cout &lt;&lt; "starting MedianSort...\n";////////////
      int temp;
      static int sort [] = { left, middle, right };
      do {
              if (sort[0] &lt;= sort[1] &amp;&amp; sort[1] &lt;= sort[2]) {
                  break;
              }
              else if (sort[1] &lt; sort[0]) {
                  temp = sort[1];
                  sort[1] = sort[0];
                  sort[0] = temp;
              }
              else if (sort[2] &lt; sort[1]) {
                  temp = sort[2];
                  sort[2] = sort[1];
                  sort[1] = temp;
              }
  
      } while(true);
  
      cout &lt;&lt; "...returning sort from Median Sort\n";//////////
      return sort;
  }
  </code></pre>
  
  <p>An example of its implementation in my partitioning algorithm:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>        int *temp;
          int tempInt;
  
          temp = MedianSort(*(array + left), *(array + pivotIndex), *(array + right));
          *(array + left) = *(temp + 0);
          *(array + pivotIndex) = *(temp + 1);
          *(array + right) = *(temp + 2);
  
          tempInt = *(array + pivotIndex); 
          *(array + pivotIndex) = *(array + left);
          *(array + left) = tempInt;
  </code></pre>
  
  <p><code>array</code> is a pointer to an array that I am quicksorting, <code>left</code> is the smallest index being passed into the partition function, and <code>right</code> is the largest. </p>
  
  <p>When I compile the code, it goes without a problem, but as soon as I run the executable, I get this error in the console: </p>
  
  <pre class="lang-sh prettyprint-override"><code>Fatal error: glibc detected an invalid stdio handle
  Aborted
  </code></pre>
  
  <p>I don't understand why, because it finishes with the 3rd input file out of 5, but doesn't even start the 4th file. (I know this because I have <code>cout</code> statements at the beginning almost every function to show me how far it is getting.)</p>
  
  <p>Also, <code>main.cpp</code> has been successfully going through all the files up to this point.</p>
  
  <p>EDIT:
  I am compiling with <code>-g -std=c++11</code></p>