简体   繁体   English

std :: array-'大小为n的缓冲区将被溢出',仅在VS中

[英]std::array - 'buffer of size n will be overrun', only in VS

constexpr auto CHUNKS_X = 5, CHUNKS_Y = 5, CHUNKS_Z = 1;
std::array<std::bitset<CHUNKS_X>, CHUNKS_Y> ys;
std::array<decltype(ys), CHUNKS_Z> zs;
if (CHUNKS_Z > 1)
{
    zs[0] = ys;
    //zs.at(1) = ys; //this works
    zs[1] = ys; //this doesn't work
    for (auto &x : zs[1])
    {
        x.flip();
    }
    for (auto z = 2; z < CHUNKS_Z; z++)
    {
        zs[z] = zs[z - 2];
    }
}

The line zs[1] = ys; zs[1] = ys; gives me 给我

error C4789: buffer 'zs' of size 20 bytes will be overrun; 20 bytes will be written starting at offset 20

But only when compiling in VS. 但仅当在VS中编译时。 Compiling on the command line gives me no such error, nor does using zs.at(1) = ys; 在命令行上编译不会给我这样的错误,使用zs.at(1) = ys;也不会给我这样的错误zs.at(1) = ys; instead. 代替。 Another thing of note is that MSDN says that this should be a warning, not an error. 值得注意的另一件事是MSDN说这应该是警告,而不是错误。

I realize this might be a subtle compiler flag issue but I haven't the slightest clue where to start looking. 我意识到这可能是一个微妙的编译器标志问题,但是我没有丝毫线索可以从哪里开始寻找。

Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x86
std::array<decltype(ys), CHUNKS_Z> zs;

is equivalent to 相当于

std::array<decltype(ys), 1> zs;

meaning zs is an array with 1 element; 表示zs是一个包含1个元素的数组;

arrays in general are zero-based meaning the first element is at zs[0] , and there are no more elements so zs[1] requires that the arrays will have a second element which it doesn't in your example. 数组通常是从零开始的,这意味着第一个元素位于zs[0] ,并且没有其他元素,因此zs[1]要求数组将具有第二个元素,而在您的示例中则没有。

Since you check if (CHUNKS_Z > 1) there should not be any problem since you can't reach the line zs[1] = ys; 因为您检查if (CHUNKS_Z > 1)是否应该没有问题,因为您无法到达zs[1] = ys; and you won't get any issues. 而且不会有任何问题。

If I change CHUNKS_Z to 2 I don't get any errors in VS15 如果我将CHUNKS_Z更改为2 ,则在VS15中没有任何错误

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM