Can I ignore a SIGFPE resulting from division by zero?

Posted by Mikeage on Stack Overflow See other posts from Stack Overflow or by Mikeage
Published on 2009-01-07T08:05:08Z Indexed on 2010/05/29 10:02 UTC
Read the original article Hit count: 338

Filed under:
|
|
|

I have a program which deliberately performs a divide by zero (and stores the result in a volatile variable) in order to halt in certain circumstances. However, I'd like to be able to disable this halting, without changing the macro that performs the division by zero.

Is there any way to ignore it?

I've tried using

#include <signal.h>
...
int main(void) {
  signal(SIGFPE, SIG_IGN);
  ...
}

but it still dies with the message "Floating point exception (core dumped)".

I don't actually use the value, so I don't really care what's assigned to the variable; 0, random, undefined...

EDIT: I know this is not the most portable, but it's intended for an embedded device which runs on many different OSes. The default halt action is to divide by zero; other platforms require different tricks to force a watchdog induced reboot (such as an infinite loop with interrupts disabled). For a PC (linux) test environment, I wanted to disable the halt on division by zero without relying on things like assert.

© Stack Overflow or respective owner

Related posts about posix

Related posts about signal