我们刚刚从 VS2010 迁移到 VS2013,我发现了一个奇怪的错误,我想知道这可能是由于编译器造成的。
使用命令行编译cl ConsoleApplication1.cpp /EHa /fp:strict /O2
以下程序给出:
0xC0000005: Access violation reading location 0xFFFFFFFF.
这仅在编译为 32 位(不是 64 位)时发生
#include <iostream>
#include <cmath>
class Vector2D
{
public:
double x;
double y;
Vector2D() : x(0), y(0) {}
Vector2D(double _x, double _y) : x(_x), y(_y) {}
double Width() { return x; }
double Height() { return y; }
};
bool IsEqual(const double & a, const double & b)
{
if (a == b)
return true;
double tolerance = pow(10., -5);
if (::fabs(a) < tolerance / 2.)
{
return ::fabs(b) < tolerance / 2.;
}
double diff = ::fabs((b - a) / a);
return (diff < tolerance);
}
bool IsEqual(Vector2D & a, Vector2D & b)
{
return IsEqual(a.Width(), b.Width()) && IsEqual(a.Height(), b.Height());
}
std::string GetMsg()
{
return std::string("");
}
int main(int argc, char* argv[])
{
Vector2D v1;
Vector2D v2;
v1 = Vector2D(1, 0);
// This innocent call will cause an access violation
// the access violation occurs *only* if fp:strict and /EHa switches are used
GetMsg(), IsEqual(v1, v2);
return 0;
}
我在指责编译器快速吗?