危险的错误 Visual c++ 2005

Dangerous error Visual c++ 2005

本文关键字:c++ 2005 Visual 错误 危险      更新时间:2023-10-16

我在运行C++Win32控制台应用程序时遇到Visual Studio 2005时遇到了一个非常严重的错误。使用以下项目属性运行下面的代码(简化)时,将显示此问题:C++|优化|优化|/O2(或/O1,或/Ox),C++|优化|全程序优化|/GL, linker|optimization|/ltcg

    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    const int MAXVAL=10;
    class MyClass
    {
    private:
      int p;
      bool isGood;
    public:
      int SetUp(int val);
    };
    int MyClass::SetUp(int val)
    {
      isGood = true;
      if (MAXVAL<val)
      {
        int wait;
        cerr<<"ERROR, "<<MAXVAL<<"<"<<val<<endl;
        cin>>wait;
        //exit(1);      //for x64 uncomment, for win32 leave commented
      }
      if (isGood) p=4;
      return 1;
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
      int wait=0, setupVal1=10, setupVal2=12;
      MyClass classInstance1;
      MyClass classInstance2;
      if (MAXVAL>=setupVal1) classInstance1.SetUp(setupVal1);
      if (MAXVAL>setupVal2) classInstance2.SetUp(setupVal2);
      cerr<<"exit, enter value to terminaten";
      cin>>wait;
      return 0;
    }
输出显示值 10

小于值 10!我已经发现将设置/O2 更改为/Od 可以解决问题(设置/Og,这是/O2 的一部分,会导致问题),但这确实会减慢执行时间。稍微更改代码也可以解决它,但是嘿,我永远无法确定代码是否可靠。我正在使用Visual Studio 2005专业版(版本8.0.50727.867),操作系统Windows 7。我的问题是:有人可以尝试使用Visual Studio 2005重现此错误吗(我已经尝试过VS 2010,没问题),如果是这样,这里会发生什么?我可以假设较新的版本已经解决了这个问题(我考虑购买VS 2012)谢谢

您可以显着减少示例,但仍然会遇到相同的问题!您不需要两个实例,也不需要任何其他局部或成员变量。此外,您可以硬编码MAXVAL .

快速总结什么"解决"了问题:

  • 使MAXVAL成为非常int
  • setupVal2设置为小于 10 的值
  • 令人惊讶的是,将条件10<val更改为val>10!!

这是我重现该问题的最小版本:

#include "stdafx.h"
#include <iostream>
using namespace std;
class MyClass
{
public:
    int SetUp(int val);
};
int MyClass::SetUp(int val)
{
    if (10<val)
        cout<<10<<"<"<<val<<endl;
    return 1;
}
int _tmain(int argc, _TCHAR* argv[])
{
    int setupVal1=10, setupVal2=12;
    MyClass classInstance;
    classInstance.SetUp(setupVal1);
    classInstance.SetUp(setupVal2);
    cin.get();
    return 0;
}

正如反汇编所见证的那样,问题在于编译器认为10<val总是正确的,因此省略了检查。

_TEXT   SEGMENT
?SetUp@MyClass@@QAEHH@Z PROC                ; MyClass::SetUp
; _val$ = ecx
; 16   :    if (10<val)
; 17   :        cout<<10<<"<"<<val<<endl;
    mov eax, DWORD PTR __imp_?endl@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@1@AAV21@@Z
    push    eax
    push    ecx
    mov ecx, DWORD PTR __imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A
    push    10                  ; 0000000aH
    call    DWORD PTR __imp_??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@H@Z
    push    eax
    call    ??$?6U?$char_traits@D@std@@@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@PBD@Z ; std::operator<<<std::char_traits<char> >
    add esp, 4
    mov ecx, eax
    call    DWORD PTR __imp_??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@H@Z
    mov ecx, eax
    call    DWORD PTR __imp_??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@P6AAAV01@AAV01@@Z@Z
; 18   :    return 1;
    mov eax, 1
; 19   : }