VS2019: [C6386] Buffer Overrun while to

  buffer, buffer-overrun, c++, visual-c++
void Counting_Sort(vector<int>& A)
{
const int size = A.size();

int max = A[0];
for (int i = 1; i < size; i++)
    if (max > A[i])
        max = A[i];

int* C = new int[max + 1]{ 0 };
for (int i = 0; i < max + 1; i++)
    C[A[i]] = C[A[i]] + 1;

for (int i = 1; i < max + 1; i++)
    C[i] = C[i] + C[i - 1];

int* B = new int[size];
for (int i = size-1; i >= 0; i--)
    B[C[A[i]] - 1] = A[i];   // <-- Warning here

}

I’m not really sure why I get the warning or what exactly it means. Setting size-1 in for loop to size-2 removes the warning, but I don’t uderstand why.

Source: Windows Questions C++

LEAVE A COMMENT