危险的错误 Visual c++ 2005
Dangerous error Visual c++ 2005
我在运行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 : }
相关文章:
- 从 MFC C++ 6.0 应用程序迁移到 VS 2005
- VS 2017 C++编译器在 VS 2005 中找不到匹配函数
- C++ LNK 2005 错误"已在 .obj 中定义
- 对于我在 Microsoft Visual C++ IDE (2005 或更高版本)的本机 win32 项目中编写的应用
- 我的C 程序在CodeBlocks中提供了正确的结果,但在Visual Basic 2005 Express Edi
- 在 VC++ 2005 中将 crlf 添加到 WCHAR * 中
- 无法在Visual Studio 2005中构建Qt项目
- 是否可以在Windows 8环境上在Visual Studio 2005上运行VC ,Win32应用程序
- Intellisense中缺少的功能定义C 2005-2013
- 在尝试超载&lt;&lt;时链接错误2005和1169操作员
- 如何使用 Visual Studio 2005 获取两个不同 c++ 程序之间的返回值
- 将 coverity SA 与 Visual Studio 2005 集成的步骤以及如何运行相同的步骤
- 用于在 Visual Studio 2005 中编译 Win32 "hello world"的命令行
- 旧版 MFC 代码 (2005) 无法在 VC 2010 上编译
- LNK 2005链接器出现msvcrt.lib(MSVCR90.dll)问题
- Visual Studio C++ 2005/2010 with boost 1.50: warning C4267
- 错误 LNK 2005 和 LNK1169 C++ Visual Studio DLL
- 将C++代码从VS 2005转换为VS 2008?这甚至意味着什么
- 如何在 Windows for Visual Studio 2005 上设置 WXWIDGETS
- MSBuild、未解析的外部项和Visual Studio 2005