Visual C ++ /启用浮点异常后的奇怪行为(编译器错误?) [英] Visual C++ / Weird behavior after enabling floating-point exceptions (compiler bug ?)

查看:292
本文介绍了Visual C ++ /启用浮点异常后的奇怪行为(编译器错误?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio(2005或2008)下,我正在努力获得可靠的方法来捕捉浮点异常。默认情况下,在视觉工作室下,浮点异常不会被捕获,并且它们很难捕获(主要是因为它们大部分是硬件信号,需要被转换为异常)

I am struggling to get a reliable way to catch floating points exceptions under Visual Studio (2005 or 2008). By default, under visual studio, floating point exceptions are not caught, and they are quite hard to catch (mainly because most of them are hardware signal, and need to be translated into exceptions)

这是我做的:

- 打开SEH异常处理

(属性/代码生成/启用C ++异常:是SEH异常)

- 使用_controlfp激活浮点异常

Here is what I did :
- Turn on SEH exceptions handling
(properties / code generation / Enable C++ Exceptions : Yes with SEH Exceptions)
- Activate floating points exceptions using _controlfp

我现在抓住异常(如下面的例子所示,这是一个简单的除以零异常)。
但是,一旦我捕获这个异常,似乎程序是不可挽回的损坏(因为简单的浮动初始化,以及std :: cout将无法正常工作!)。

I do now catch the exceptions (as shown in the example below which a simple divide by zero exception). However, as soon as I catch this exception, it seems that the program is irremediably corrupted (since simple float initialization, as well as std::cout won't work!).

我已经建立了一个简单的演示程序,显示出这个相当奇怪的行为。

I have built a simple demo program that shows this rather weird behavior.

注意:这种行为是在几台电脑上复制的。

Note : this behavior was reproduced on several computers.

#include "stdafx.h"
#include <math.h>

#include <float.h>
#include <iostream>


using namespace std;


//cf http://www.fortran-2000.com/ArnaudRecipes/CompilerTricks.html#x86_FP
//cf also the "Numerical Recipes" book, which gives the same advice 
    //on how to activate fp exceptions
void TurnOnFloatingExceptions()
{
  unsigned int cw;
  // Note : same result with controlfp
  cw = _control87(0,0) & MCW_EM;
  cw &= ~(_EM_INVALID|_EM_ZERODIVIDE|_EM_OVERFLOW);
  _control87(cw,MCW_EM);

}

//Simple check to ensure that floating points math are still working
void CheckFloats()
{
  try
  {
         // this simple initialization might break 
         //after a float exception!
    double k = 3.; 
    std::cout << "CheckFloatingPointStatus ok : k=" << k << std::endl;
  }  
  catch (...)
  {
    std::cout << " CheckFloatingPointStatus ==> not OK !" << std::endl;
  }
}


void TestFloatDivideByZero()
{
  CheckFloats();
  try
  {
    double a = 5.;
    double b = 0.;
    double c = a / b; //float divide by zero
    std::cout << "c=" << c << std::endl; 
  }
  // this catch will only by active:
  // - if TurnOnFloatingExceptions() is activated 
  // and 
  // - if /EHa options is activated
  // (<=> properties / code generation / Enable C++ Exceptions : Yes with SEH Exceptions)
  catch(...)
  {         
    // Case 1 : if you enable floating points exceptions ((/fp:except)
    // (properties / code generation / Enable floting point exceptions)
    // the following line will not be displayed to the console!
    std::cout <<"Caught unqualified division by zero" << std::endl;
  }
  //Case 2 : if you do not enable floating points exceptions! 
  //the following test will fail! 
  CheckFloats(); 
}


int _tmain(int argc, _TCHAR* argv[])
{
  TurnOnFloatingExceptions();
  TestFloatDivideByZero();
  std::cout << "Press enter to continue";//Beware, this line will not show to the console if you enable floating points exceptions!
  getchar();
}



<
非常感谢您提前!

Does anyone have a clue on what could be done to correct this situation? Many thanks in advance!

推荐答案

您必须清除状态字中的FPU异常标记,当您抓住浮点异常。调用_clearfp()。

You have to clear the FPU exception flags in the status word when you catch a floating point exception. Call _clearfp().

考虑使用_set_se_translator()来编写将硬件异常转换为C ++异常的异常过滤器。一定要有选择性,只能翻译FPU例外。

Consider using _set_se_translator() to write an exception filter that translate the hardware exception to a C++ exception. Be sure to be selective, only translate the FPU exceptions.

这篇关于Visual C ++ /启用浮点异常后的奇怪行为(编译器错误?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆