铸造整数函数信号code指针 - 为什么这项工作? [英] Casting integer to function pointer in signal code - why does this work?

查看:103
本文介绍了铸造整数函数信号code指针 - 为什么这项工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了一本书(高级编程在UNIX环境)和我会通过信号的部分。

I'm reading a book (Advanced Programming in the UNIX Environment) and I'm going through the section on signals.

在使用信号功能:

void (*signal(int signo, void (*func)(int)))(int);

参数,FUNC,可以是一个指针,指向由用户定义的函数,或者它可以是SIG_ERR,SIG_DFL或SIG_IGN

the parameter, func, can be a pointer to a function defined by the user, or it can be SIG_ERR, SIG_DFL, or SIG_IGN.

我的问题是不是在此UNIX的一部分,但我想给背景。我真正想知道的是,这本书指出,这些常量defiend为:

My question isn't on the UNIX part of this, but I wanted to give background. What I really want to know is, the book states that these constants are defiend as:

#define SIG_ERR (void (*)())-1

和类似物为0,1

现在,我有一些体面的猜测,但为了节省时间 - 有人可以告诉我究竟发生了什么这是干什么,为什么它的工作原理

Now, I have some decent guesses but to save time - can someone tell me what the heck this is doing and why it works?

另外,有没有......呃...清洁?方式来写这个假设我用C ++以及与此C API进行交互?

Also, is there a... erm... cleaner? way to write this assuming I'm using C++ and interacting with this C API?

推荐答案

它转换为一个函数指针的整数; * 占位符值psumably选择,使他们永远与碰撞$ P $真正的函数指针的值。 信号的实施 presumably然后检查这些特殊值的输入参数。

It's casting an integer to a function pointer;* the placeholder values are presumably chosen so that they could never collide with the values of real function pointers. The implementation of signal presumably then checks the input argument for these special values.



<子> * C标准允许这种(见6.3.2.3第5页),但它说,结果是实现定义的。显然,操作系统的作者能够控制这个实现定义方面,所以一切都ok了。

这篇关于铸造整数函数信号code指针 - 为什么这项工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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