title: Why do I get 0xC0000374 (STATUS_HEAP_CORRUPTION) error when using qsort() function of std in C++, and how do I fix it?
body: <p>It's a simple C++ test code, it will just sort the student structs by their studentId which is an integer:</p>
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  struct student {
      int grade;
      int studentId;
      string name;
  };
  
  int studentIdCompareFunc(const void * student1, const void * student2);
  
  
  int main()
  {
      const int ARRAY_SIZE = 10;
      student studentArray[ARRAY_SIZE] = {
          {81, 10009, &quot;Aretha&quot;},
          {70, 10008, &quot;Candy&quot;},
          {68, 10010, &quot;Veronica&quot;},
          {78, 10004, &quot;Sasha&quot;},
          {75, 10007, &quot;Leslie&quot;},
          {100, 10003, &quot;Alistair&quot;},
          {98, 10006, &quot;Belinda&quot;},
          {84, 10005, &quot;Erin&quot;},
          {28, 10002, &quot;Tom&quot;},
          {87, 10001, &quot;Fred&quot;},
      };
  
      qsort(studentArray, ARRAY_SIZE, sizeof(student), studentIdCompareFunc);
  
      for (int i = 0; i &lt; ARRAY_SIZE; i++)
      {
          cout &lt;&lt; studentArray[i].studentId &lt;&lt; &quot; &quot;;
      }
  
  }
  
  
  int studentIdCompareFunc(const void * voidA, const void * voidB)
  {
      student* st1 = (student *) (voidA);
      student* st2 = (student *) (voidB);
      return st1-&gt;studentId - st2-&gt;studentId;
  }
  </code></pre>
  <p>It prints the studentId integers as expected, but doesn't return zero, it returns -1072740940 (0xC0000374).</p>
  <p>I tested by changing ARRAY_SIZE to 15, or increasing ARRAY_SIZE argument, but I still get the same result.</p>
  <p>What's the cause of this error? How do I fix it?</p>