【问题标题】:Floating point exception caused by assigning a value to variable of type double为 double 类型的变量赋值导致的浮点异常
【发布时间】:2014-02-27 22:33:38
【问题描述】:

程序收到信号 SIGFPE,算术异常。 xxx::init (this=0xbffe47fc, aa=0x0) at s.cc:1061 1061价格=100.0;

我只是尝试在不同的Linux机器RH5.6 32bit上编译和运行代码而不做任何修改,这个应用程序的所有者在RH5.3上编译它没有问题。

gdb bt

B::init

b_init

B::B

A::A

主要

这里是代码

class A : public B
{
A () : a_1(1)
{
    init()
}

void init();

int a_1;
};

class B
{
double price;

B() 
{
memset(this, 0, sizeof(*this);
b_init(this);
}

int b_init( B* b)
{
return b->init();
}

void init()
{
price = 100.0;
}
};

int main()
{
A a;
}

我看起来很正常。任何人都可以阐明它吗?谢谢!

【问题讨论】:

  • 我们需要看看剩下的代码。 init 是如何被调用的? aStruct 怎么了?
  • init() 中缺少参数??虽然它可能不是错误的确切原因。
  • 请发一个真实的程序。我可以通过这样做得到类似的错误:xxx *px; px->init();
  • 我发布了更详细的示例代码。如果我在 price = 100.0 之前插入 double temp = 1.1,那么这条线将导致 FPE。

标签: c++ linux exception sigfpe


【解决方案1】:

这个测试代码对我来说很好用。一定有别的事情发生了?

#include <iostream>
using namespace std;

struct teststruct
{
  int a;
  double b;
};

class test_class
{
public:
  void init( struct teststruct * );
  double getPrice();
private:
  double price;
};

void test_class::init( struct teststruct *test )
{
  price = 100.0;
}

double test_class::getPrice()
{
  return price;
}

int main ()
{
  struct teststruct a;
  test_class test;

  test.init(&a);
  cout << "Double " << test.getPrice() << endl;

  return 0;
}

输出:

[hendric@Linux-Test-System ~]$ g++ doubletest.cpp 
[hendric@Linux-Test-System ~]$ ./a.out
Double 100

【讨论】:

  • 谢谢!我已将代码放入一个简单的测试函数中,它工作正常。但实际应用的问题仍未解决。那里的代码行太多。
猜你喜欢
  • 2020-06-26
  • 1970-01-01
  • 1970-01-01
  • 2020-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多