我们刚刚从VS2010移到VS2013,我发现了一个奇怪的错误,我不知道它可能是由于编译器。VS2013:float和/ EHa +/fp的编译器bug:strict?
编译的命令行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;
}
我是否快速指责编译器时,会发生?
代码在我眼中显得很好。我会去编译器的bug。保持删除位以最小化代码,并报告错误。你可以从'foo'和'height'中使用字符串,并简化'IsEqual'。 –
'''''绑定到'std :: string&'是未定义的行为,删除那个位并确认错误仍然发生 –
@MooingDuck错误仍然存在 – BenjaminB