简体   繁体   中英

Stopping the debugger when a NaN floating point number is produced without a code change

I read this and this . The quintessence is that one can throw a SIGFPE if a nan is produced by including fenv.h and enabling all floating point exceptions but FE_INEXACT by feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT);

Thus, the code changes form

int main () {
   double dirty = 0.0;
   double nanvalue = 0.0/dirty;
   return 0;
 }

to

 #include <fenv.h>
 int main () {
     feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT);  // Enable all floating point exceptions but FE_INEXACT
     double dirty = 0.0;
     double nanvalue = 0.0/dirty;
     return 0;
 }

This works fine, but you have to change the code. I have the problem, that in a huge c and c++ code base, somewhere a nan is produced and I don't know where. It is not an option to apply the above change to hunderts of files and track the error.

Is there a way to enable the all floating point exceptions, WITHOUT a code change? Is there a compile option I am not aware of?

We use the intel icc version 15.0.3 compiler.

No matter how many files your code spans, you only need to add feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT) once only, at the first line of your main() function.

It will enable the exceptions for your whole program until you disable the exceptions by calling another function such as fedisableexcept() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM