简体   繁体   English

VS2019:[C6386] 缓冲区溢出而到

[英]VS2019: [C6386] Buffer Overrun while to

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.将 for 循环中的 size-1 设置为 size-2 会删除警告,但我不明白为什么。

I notice four separate issues with your sample code:我注意到您的示例代码有四个不同的问题:

  1. The computation of maximum is incorrect.最大值的计算不正确。 Your condition should be testing if (A[i] > max)您的情况应该是测试if (A[i] > max)
  2. The Counting Sort algorithm's "accumulation step" should be iterating over the input data.计数排序算法的“累积步骤”应该迭代输入数据。 That is, the loop should be the following (up to size , not up to max + 1 ):也就是说,循环应该如下(最多为size ,而不是最多max + 1 ):
for (int i = 0; i < size; i++)
    C[A[i]] = C[A[i]] + 1;
  1. The algorithm's final loop forgot to update the destination of each "Counting Sort bin".算法的最终循环忘记更新每个“计数排序箱”的目的地。 That is, the final loop should be the following:也就是说,最终的循环应该如下:
for (int i = size - 1; i >= 0; i--) {
    B[C[A[i]] - 1] = A[i];
    C[A[i]] = C[A[i]] - 1;
}
  1. Don't forget to use delete[] on B and C when you are done with them.完成后不要忘记在BC上使用delete[]

Here is the fully edited result:这是完全编辑的结果:

#include <iostream>
#include <vector>

void Counting_Sort(std::vector<int>& A) {
    const int size = A.size();

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

    int* C = new int[max + 1]{ 0 };
    for (int i = 0; i < size; 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];
        C[A[i]] = C[A[i]] - 1;
    }

    for (int i = 0; i < size; i++)
        std::cout << "B[" << i << "] is " << B[i] << "\n";

    delete[] B;
    delete[] C;
}

int main() {
    std::vector<int> A = {6, 1, 3, 3, 6, 9};
    Counting_Sort(A);
    return 0;
}

The compliler says that the size of B is 4xsize bytes long but in some cases, 8 bytes might be written, which might overpass the 4xsize bytes length.编译器说 B 的大小是 4xsize 字节长,但在某些情况下,可能会写入 8 个字节,这可能会超过 4xsize 字节的长度。

Take the example of A = {10} for instance.以 A = {10} 为例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VS2015:[C6386] 写入时缓冲区溢出(即使对于相同的索引值) - VS2015: [C6386] Buffer Overrun while writing (even for same index value) 避免“缓冲区溢出” C6386警告 - Avoiding 'Buffer Overrun' C6386 warning __m256 阵列上的 Visual Studio 2019 C6385 / C6386(缓冲区溢出警告) - Visual Studio 2019 C6385 / C6386 (buffer overrun warning) on __m256 array 为什么我在写入“ptr”时收到“C6386”缓冲区溢出警告? - Why am I getting warning 'C6386' Buffer overrun while writing to 'ptr'? VS2019:[C6386][C6001] 修改计数排序的警告 - VS2019: [C6386][C6001] warnings on modified counting sort 错误:C6386:写入“newArr”时缓冲区溢出:可写大小为“int current_size*1”字节,但可能写入“2”字节 - Error: C6386: Buffer overrun while writing to 'newArr': the writable size is 'int current_size*1' bytes, but '2' bytes might be written Visual Studio 2015 代码分析 C6386 警告缓冲区溢出 - Visual Studio 2015 Code Analysis C6386 warns of buffer overrun 为什么使用strsafe.h StringCch函数会导致C6386缓冲区溢出错误? - Why error C6386 buffer overrun with strsafe.h StringCch functions? 使用动态分配的 arrays 导致来自代码分析的 C6386 缓冲区溢出警告 - Using dynamically-allocated arrays causes C6386 Buffer Overrun warning from Code Analysis 为什么在Visual Studio 2012的代码分析中,此代码为什么发出缓冲区溢出警告(C6385 / C6386)? - Why does this code emit buffer overrun warnings(C6385/C6386) in code analysis on Visual Studio 2012?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM