c++ - 可能的 VS2012 编译器错误(可能在整个程序优化中?)

标签 c++ visual-studio-2012 compiler-optimization compiler-bug

这可能是编译器错误吗?我的环境是:

  • Win7 专业版(64 位)
  • VS2012(更新 3)

我编译了下面的微型控制台程序。 x64 位发布/调试构建工作正常。 x32 调试版本也可以正常工作。 x32 发布版本,但是显示“BUG!”。

如果我禁用“全程序优化”将解决问题。

有什么想法吗?

-

#include <string>
#include <iostream>


int main()
{
    std::string const buffer = "hello, world";
    std::string::size_type pos = 0;
    std::string::size_type previous_pos;


    while (pos != std::string::npos)
    {
        previous_pos = ++pos;
        pos = buffer.find('w', pos);
    } 


    if (previous_pos == std::string::npos)
    {
        std::cout << "BUG!!"<< std::endl;
    }

    return 0;
}

最佳答案

我也可以复制这个。当错误出现时,代码会测试 eax 以确定是否输出“BUG”,这是用于“pos”的同一个寄存器。

17:         previous_pos = ++pos;

013C12E5 包含 eax
...

21:     if (previous_pos == std::string::npos)

00301345 cmp eax,eax
00301347 jne main+0F6h (0301366h)

但是,如果您进行更改以尝试让优化器意识到它们是不同的,那么测试就不同了。如果我在循环体的末尾添加++previous_pos 然后它使用 ecx 作为 previous_pos 并且错误消失了:

22:     if (previous_pos == std::string::npos)

00361349 cmp ecx,eax
0036134B 主+0FAh (036136Ah)

如果我将查找更改为 'pos = buffer.find('w', previous_pos);' (从 previous_pos 而不是具有相同值的 pos 搜索)然后它使用 ebx,错误再次消失:

21:     if (previous_pos == std::string::npos)

00191345 cmp ebx,eax
00191347 jne main+0F6h (0191366h)

所以在原文中似乎优化器错误地决定它可以将 eax 用于这两个变量,尽管行 'pos = buffer.find('w', pos);'可以将 pos 设置为与 previous_pos 不同的值。

关于c++ - 可能的 VS2012 编译器错误(可能在整个程序优化中?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18048705/

相关文章:

c - 使用 uint16_t* 访问 uint32_t 数组会导致未定义的行为吗?

c - 需要帮助优化函数调用

c++ - 使用 float 给出 "call to overloaded function is ambiguous"错误

c++ - 是>?或 <?任何 C++ 方言中的合法运算符?

visual-c++ - 如何使用MSBuild定位v110平台工具集?

visual-studio - 如何禁用可能由Visual Studio生成的MSI日志文件

c++ - 使用汇编加速位测试操作

c++ - 这个宏可以转换成一个函数吗?

c++ - 部署使用 C++ 开发的应用程序

visual-studio-2012 - 构建解决方案时 Typescript 文件不会编译 (Visual Studio 2012)