我可以忽略除以零所产生的SIGFPE吗? [英] Can I ignore a SIGFPE resulting from division by zero?

查看:144
本文介绍了我可以忽略除以零所产生的SIGFPE吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序故意将其除以零(并将结果存储在volatile变量中),以便在某些情况下停止运行.但是,我希望能够禁用此暂停功能,而无需更改执行除以零的宏.

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?

我尝试使用

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

,但是它仍然死于消息浮点异常(内核已转储)".

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

我实际上并没有使用该值,所以我真的不在乎分配给变量的内容. 0,随机,不确定...

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

我知道这不是最便携式的,但是它是针对运行在许多不同OS上的嵌入式设备而设计的.默认的暂停动作是除以零;其他平台则需要不同的技巧来强制看门狗引起重启(例如,禁用了中断的无限循环).对于PC(linux)测试环境,我想禁用除以零的停止功能,而不依赖于assert之类的东西.

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.

推荐答案

为什么要故意执行除以零的操作来停止?您不能exit(-1)或其他等效语言吗?

Why would you deliberately perform a divide by zero to halt? Couldn't you exit(-1) or some equivalent?

特别是如果您不需要除以0的结果,那就没有意义了.

Especially if you don't need the result of the divide by 0, this just doesn't make sense.

这篇关于我可以忽略除以零所产生的SIGFPE吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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