title: visualstudio malloc ERROR_INVALID_ACCESS
body: <pre><code>#include "stdafx.h"
  #include &lt;errno.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;malloc.h&gt;
  int main()
  {
      int i = 1000;
      int count = 1;
      int newlen = 1;
      int oldlen = 10;
      char *result = NULL;
      int total = 0;
  
      /* case 1 */
      total = i + 1 + count * (newlen - oldlen);
      printf("case 1 start\n");
      result = (char *)malloc(total);
      if (result == NULL)
      {
          return -1;
      }
      free(result);
      printf("case 1 success\n");
  
  
      /* case 2 */
      printf("case 2 start\n");
      result = (char *)malloc(i + 1 + count * (newlen - oldlen));
      free(result);
      if (result == NULL)
      {
          return -1;
      }
      printf("case 2 success\n");
      return 0;
  }
  </code></pre>
  
  <p>Why has <code>malloc()</code> failed?</p>
  
  <p>maybe <code>(newlen - oldlen)</code> is negative or <code>count * (minus number)</code>?</p>