避免'缓冲区溢出'C6386警告

时间:2010-06-11 10:43:16

标签: c++ windows visual-c++ warnings buffer-overrun

在我的代码中,我使用的是10个对象的数组xyz。当我尝试使用unsigned int index访问数组的元素时,如下所示:xyz[level],我收到'Buffer overrun'警告。从逻辑上讲,我很确定该级别不会超过10.如何避免此警告?

2 个答案:

答案 0 :(得分:9)

我可能会教我的祖母在这里吮吸鸡蛋,但要记住“水平不会超过10”对于10号阵列是错误的:

char a[10];
a[10] = '\0';  // Bug, and "Buffer Overrun" warning.

答案 1 :(得分:0)

真的确定吗?直到现在我才收到这个警告。所以,仔细检查。

无论如何,你可以使用

#pragma warning( disable: 6386 )

预处理程序指令。我通常将其推送到“pragma stack”

#pragma warning( push )
#pragma warning( disable : 6386 )
// Some code
#pragma warning( pop )

建议here

相关问题