Code Bye

signal()返回值SIG_ERR的定义

 

大家好。
signal函数的原型是void (*signal(int signum, void (*handler))(int)))(int);
返回的是一个void (*)(int)的函数指针
#define SIG_ERR (void (*)())-1
#define SIG_DFL (void (*)())0
#define SIG_IGN (void (*)())1
这个SIG_ERR是void(*)(),这里没有int呀
这里应该怎么理解呢?

signal返回的是一个返回值为void型且参数为int型的函数指针
而SIG_ERR是把1强制转换为void型没参数的函数指针

这样没影响吗?

10分
这才是c语言, 可以很无耻的这样转。这样转换就是把0,-1,1看做了地址。
signal的返回值本意是返回上次设置的signal处理函数,用于备份和恢复。
/* This example installs a signal handler routine for SIGFPE,
catches an integer overflow condition, makes an adjustment
to AX register, and returns. This example program MAY cause
your computer to crash, and will produce runtime errors
depending on which memory model is used.
*/
#pragma inline
#include <stdio.h>
#include <signal.h>
void Catcher(int sig, int type, int *reglist)
{
printf(“Caught it!\n”);
*(reglist + 8) = 3; /* make return AX = 3 */
}
int main(void)
{
signal(SIGFPE, Catcher);
asm mov ax,07FFFH /* AX = 32767 */
asm inc ax /* cause overflow */
asm into /* activate handler */
/* The handler set AX to 3 on return. If that hadn””t happened,
there would have been another exception when the next “”into””
was executed after the “”dec”” instruction. */
asm dec ax /* no overflow now */
asm into /* doesn””t activate */
return 0;
}
还有这里Catcher函数有3个参数,signal也能执行?
是不是void(*handle)(int)只看成指针,传地址就行?形参并不进行检查?
10分
你确定这个能正确执行, 编译就过不了,不能这样用。
20分
vs2012里给的定义是
#define SIG_ERR (void (__cdecl *) (int))-1
其它的也是都有int
说明是你的编译器的问题吧
不过能编译通过也不会产生错误
但是不同指针也能比较,是什么编译器啊
引用 3 楼 a30037338 的回复:

你确定这个能正确执行, 编译就过不了,不能这样用。

我是直接百度signal函数,有的百科介绍这函数的时候引用了上面的例子。
手机上网查的,没验证。

引用 4 楼 ID870177103 的回复:

vs2012里给的定义是
#define SIG_ERR (void (__cdecl *) (int))-1
其它的也是都有int
说明是你的编译器的问题吧
不过能编译通过也不会产生错误
但是不同指针也能比较,是什么编译器啊

刚刚查了下源代码,确实是有int的,上面这些我都是用手机在网上查到的,看来网上的也不能尽信,得自己验证,谢谢。


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明signal()返回值SIG_ERR的定义