[英]Possible C/C++ compiler bug in Visual Studio 2013
以下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;
}
是的,它似乎是一個編譯器錯誤。 在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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.