以下C或C ++代码应具有输出“11,11,11” 但是使用Visual Studio Professional 2013(版本12.0.40629.00 Update 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;
}
答案 0 :(得分:9)
是的,它似乎是一个编译器错误。在Win32版本的代码中,编译器使用寄存器esi
来表示y
并注册edx
来表示x
。正如@Ajay Brahmakshatriya在评论中正确指出的那样,似乎编译器尝试交换周期(将外部与内部交换),但结果却是错误的代码。最后一个条件跳转指令,应该代表[交换]内部循环,由于某种原因将控制转移到检查esi
的位置。该检查过早地结束了迭代。
0018206B xor esi,esi ; This is `y`
0018206D xor edx,edx ; This is `x`
...
00182070 test esi,esi
00182072 jg main+5Ch (018209Ch) ; Exit from the outer cycle?
00182074 lea eax,[edx+esi*2] ; Recalculate the starting storage location
00182077 add eax,esi ; for the next cycle:
00182079 lea ecx,[A] ; eax = esi * 3 + edx
0018207F lea eax,[ecx+eax*4] ; eax = &A[eax]
...
00182082 mov ecx,1 ; It is not exactly clear to me what this is
00182087 sub ecx,esi ; supposed to do, but when `esi` is `0`, it
00182089 add esi,ecx ; leaves `ecx` as 1, which is correct
; number of iterations for outer cycle
...
00182090 mov dword ptr [eax],0Bh ; Storing the value
00182096 lea eax,[eax+0Ch] ; Updating the pointer for the next storage location
00182099 dec ecx
0018209A jne main+50h (0182090h) ; Outer cycle [exchanged]
0018209C inc edx
0018209D cmp edx,3
001820A0 jl main+30h (0182070h) ; Inner cycle [exchanged]: for some reason it
; jumps to `test esi,esi`, which is what
; suddenly terminates the iterations