小编Lor*_*Lin的帖子

在发布模式下,代码行为不符合预期

以下代码在调试模式和发布模式下生成不同的结果(使用Visual Studio 2008):

int _tmain(int argc, _TCHAR* argv[])
{

    for( int i = 0; i < 17; i++ ) 
    { 
        int result = i * 16;

        if( result > 255 )
        {
            result = 255;
        }

        printf("i:%2d, result = %3d\n", i, result) ; 
    } 

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

调试模式的输出,如预期的那样:

i: 0, result =   0
i: 1, result =  16
(...)
i:14, result = 224
i:15, result = 240
i:16, result = 255
Run Code Online (Sandbox Code Playgroud)

释放模式的输出,其中i:15结果不正确:

i: 0, result =   0
i: 1, result …
Run Code Online (Sandbox Code Playgroud)

c c++ optimization visual-studio-2008 compiler-bug

131
推荐指数
2
解决办法
1万
查看次数

标签 统计

c ×1

c++ ×1

compiler-bug ×1

optimization ×1

visual-studio-2008 ×1