C语言signal11,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 

#include 

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的,上面这些我都是用手机在网上查到的,看来网上的也不能尽信,得自己验证,谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值