gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:54:42 25 4
gpt4 key购买 nike

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

  • 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com