【发布时间】:2017-03-01 17:55:08
【问题描述】:
以下 C 或 C++ 代码的输出应为“11,11,11” 但是使用 Visual Studio Professional 2013(版本 12.0.40629.00 更新 5)输出为“11,0,0”!这仅发生在发布版本中,并在关闭优化时消失。这是编译器错误吗?
#include <stdio.h>
int main(void)
{
int A[100] = { 0 };
int row = 0; // BUG disappears if we make this const or short or char...
int ncols = 3; // BUG disappears if we make this const or short or char...
for (int y = row; y <= row; ++y)
{
for (int x = 0; x < ncols; ++x)
{
const int index = y * ncols + x;
//A[index] = 11; // (no bug !)
*(A + index) = 11; // BUG!!!
//*(A + y*ncols+x) = 11; // (no bug !)
//*(A + (y*ncols+x)) = 11; // BUG!!!
}
}
for (int x = 0; x < ncols; ++x)
{
printf("%d,", A[x]);
}
return 0;
}
【问题讨论】:
-
无法在 MSVC 2015 中重现。这是显示问题的 Minimal, Complete, and Verifiable example 吗?
-
我无法使用 Visual C++ 2015 更新 3 重现声称的行为。
-
@WeatherVane 检查 VS 2013。
-
只是想说,在 clang 3.9.1 上通过 -O2 运行它(我知道,与问题无关,但我很好奇)会产生 pretty funny asm result。无论团队是谁进行循环分析都应该感到自豪 =)
-
在 VS15 和 VS13 上进行了测试,包括 cpp 和 c。每次输出都是预期的。您能否向我们提供编译器的确切版本号。生成的程序集也很有趣。
标签: c++ c visual-studio-2013