title: VS2015: [C6386] Buffer Overrun while writing (even for same index value)
body: <p>I'm trying to implement merge sort in C when I came across something interesting raised by [Analyze -> Run Code Analysis] in Visual Studio 2015.</p>
  
  <p>The code is as follows:</p>
  
  <pre><code>void MergeSort_r(int A[], int n)
  {
      // A = {1, 3, 2}
      // n = 3
      int rightCount;
      int* R;
  
      if ( n &lt; 2 ) return;
  
      // version 1: rightCount = 2
      rightCount = n - (n/2);
  
      // version 2: rightCount = 2
      rightCount = n - 1;
  
      R = ( int* ) malloc( rightCount * sizeof( int ) );
  
      if ( R ) {
          for ( int i = 0; i &lt; rightCount; i++ ) {
              R[i] = A[i];
          }
  
      free( R );
      }
  
  }
  </code></pre>
  
  <p>Even though both version of rightCount essentially evaluates to 2, in the first version, I get the warning: </p>
  
  <blockquote>
    <p>"Buffer overrun while writing to 'R':  the writable size is '(unsigned int)rightCount*sizeof(int)' bytes, but '8' bytes might be written."</p>
  </blockquote>
  
  <p>Any idea why this is the case? Looking forward to hear your answers.</p>