码迷,mamicode.com
首页 > 其他好文 > 详细

SIGFPE能处理上下溢吗?

时间:2016-04-24 15:43:33      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

SIGFPE可以报告算术异常。包括上下溢、除零等。

C标准库说:

各种实现报告的异常类型区别也很大,但几乎没有实现会报告整型溢出。

真的吗?我测试了一下,发现在GCC上,整型、浮点上下溢都不会报告,但是除0会报告。在使用-O3编译选项后,连除0都不报告了,直接继续执行。

看来处理上下溢真不能靠信号——为什么编译器不提供呢?这样不是非常方便吗?

附上测试的代码,大家可以在自己的机子上试一下,看看能不能处理上下溢。

#include <signal.h>
#include <setjmp.h>
#include <stdio.h>
#include <assert.h>
#include <limits.h>
#include <time.h>

jmp_buf env;
enum TEST_STATE { OVERFLOW, INFLOW, ZERO, END }
	state_group_[] = { OVERFLOW, INFLOW, ZERO, END },
	*state = state_group_;

void handle_SIGFPE(int signo);

int main(int argc, char const *argv[])
{
	int val;
	int i; /* 改成double,如果想测试浮点上下溢。 */
	time_t time_stamp;
	signal(SIGFPE, handle_SIGFPE);

	val = setjmp(env);
	printf("Got jmp val %d.\n", val);

	switch (*state) {
	case OVERFLOW:
		printf("Do overflow.\n");
		++state;
		i = INT_MAX;
		time_stamp = time(NULL);
		while (1) {
			i++;
			if (difftime(time(NULL), time_stamp) >= 5) {
				printf("\tFailure. Timeout.\n");
				longjmp(env, 2);
			}
		}
		assert(0);

	case INFLOW:
		printf("Do inflow.\n");
		++state;
		i = INT_MIN;
		time_stamp = time(NULL);
		while (1) {
			i--;
			if (difftime(time(NULL), time_stamp) >= 5) {
				printf("\tFailure. Timeout.\n");
				longjmp(env, 2);
			}
		}
		assert(0);

	case ZERO:
		printf("Do zero.\n");
		++state;
		i = 1/0;
		assert(0);

	case END:
		printf("Do exit.\n");
		exit(0);

	default:
		assert(0);
	}

	assert(0);
	return 0;
}

void handle_SIGFPE(int signo)
{
	printf("\tSuccess. Got signal SIGFPE %d.\n", signo);
	longjmp(env, 1);
}
没用-O3的输出
Got jmp val 0.
Do overflow.
        Failure. Timeout.
Got jmp val 2.
Do inflow.
        Failure. Timeout.
Got jmp val 2.
Do zero.
        Success. Got signal SIGFPE 8.
Got jmp val 1.
Do exit.
		
用了-O3的输出
Got jmp val 0.
Do overflow.
        Failure. Timeout.
Got jmp val 2.
Do inflow.
        Failure. Timeout.
Got jmp val 2.
Do zero.
Assertion failed!

Program: C:\Users\01\Desktop\report\a.exe
File: sigfpe.c, Line 58

Expression: 0

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application‘s support team for more information.
		

SIGFPE能处理上下溢吗?

标签:

原文地址:http://www.cnblogs.com/destro/p/sigfpe.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!