指向功能的指针 [英] Pointer to functions

查看:60
本文介绍了指向功能的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道变量(void *)保证能够存储任何

指针并允许转换回原始指针。


我认为,我可以在(void *)

变量中存储指向函数的指针,然后将其转换回原始类型并将其用作

功能。


问题是:我是正确的还是可能会出现这种情况?

可能会失败?


为了澄清,这里是我正在尝试做的一个例子:


/ *在某处定义的函数原型* /

int f1(int x);

void f2(int a,int b);


/ *类型为函数指针的变量* /

int(* fa)(int);

void(* fb)(int,int);


/ *一个通用指针数组* /

void * fun_vec [2];


..... / *稍后代码中...... * /

fun_vec [0] = f1; / *存储指向f1和f1的指针* /

fun_vec [1] = f2; / *进入无效* * /


..... / *甚至更晚...... * /

fa = fun_vec [0]; / *将它们作为指针取回* /

fb = fun_vec [1]; / *到函数* /


fb(2,fa(3)); / *使用功能* /


....

解决方案

Remo D 。< rdwrote:


我知道变量(void *)保证能够存储任何

指针并允许转换回原始指针。


我认为,我可以在(void *)

变量中存储指向函数的指针然后将其转换回原始类型并将其用作

功能。


问题是:我是否正确或者有可能出现这种情况?
可能会失败?



不幸的是,C标准没有涵盖函数指针

可以转换为void指针并返回。似乎有些架构会导致问题。另一方面

在很多情况下你可能会这样做(例如在POSIX

兼容的系统上,这必须起作用,或者例如dlopen()不能'' t
使用


为了澄清,这是我正在尝试做的一个例子:


/ *在某处定义的函数原型* /

int f1(int x);

void f2(int a,int b);


/ *类型的变量指向函数的指针 * /

int(* fa)(int);

void(* fb)(int,int);


/ *一般通用指针数组* /

void * fun_vec [2];


.... / *稍后代码... * /

fun_vec [0] = f1; / *存储指向f1和f1的指针* /

fun_vec [1] = f2; / * into void * * /


.... / *甚至更晚...... * /

fa = fun_vec [0]; / *将它们作为指针取回* /

fb = fun_vec [1]; / * to functions * /


fb(2,fa(3)); / *使用函数* /



对于大多数系统,你应该这样做。请注意,

您也可以写下最后一个例如作为


fun_vec [1](2,fun_vec [0](3));


问候,Jens

-

\ Jens Thoms Toerring ___ jt@toerring.de

\\ \\ __________________________ http://toerring.de


< blockquote> Jens Thoms Toerring ha scritto:


>

不幸的是,C标准没有涵盖函数指针

可转换为void指针并返回。似乎有些架构会导致问题。另一方面

在很多情况下你可能会这样做(例如在POSIX

兼容的系统上,这必须起作用,或者例如dlopen()不能'' t
使用



谢谢Jens!我在互联网上找到了C标准(N869)的草案

,正如你所说,没有提到指向功能的指示

可转换为无效*。


但是,在第6.3.2.3节第8段中,他们说:


>指向一种类型的函数的指针可以转换为指向另一种类型的函数的指针,然后再返回;
结果将与原始指针进行比较。
如果是转换后的指针用于调用
类型与指向类型不兼容的函数,
行为未定义。



所以,如果我要声明一个函数指针数组,它应该更安全吗?


/ *函数返回int的2个指针数组* /

int(* fun_vec [2])();


一旦我将它们转换为相应的函数类型,它应该工作(如果

我正确读取标准)。


正如你正确指出的那样,只要我使用它们也应该工作

正确(我希望编译器警告)。


R:D


On Jun 19日下午5点40分,Remo D. < rdwrote:


我知道变量(void *)保证能够存储任何

指针并允许转换回到原始指针。



这是这样做的常见延伸,(见附件G ISO 9899:1999)但符合程序的
不应依赖那。


我认为,我可以在(void *)

变量中存储指向函数的指针,然后将其转换回来到原始类型并将其用作

功能。



ISO C不允许。

但是,您可以将任何函数指针转换为任何其他函数指示

函数指针和返回,并得到相同的结果。


问题是:我是否正确或有可能出现这种情况?
可能会失败?



是的,例如,如果函数指针实际上不是指针,或者

不能用void *等表示。


为了澄清,这是我正在尝试做的一个例子:


/ *函数的原型是在某处定义* /

int f1(int x);

void f2(int a,int b);


/ *类型指向函数的指针的变量。 * /

int(* fa)(int);

void(* fb)(int,int);


/ *一般的指针数组* /

void * fun_vec [2];


.... / *稍后的代码... * /

fun_vec [0] = f1; / *存储指向f1和f1的指针* /

fun_vec [1] = f2; / *进入无效* * /


.... / *甚至更晚...... * /

fa = fun_vec [0]; / *将它们作为指针取回* /

fb = fun_vec [1]; / *到函数* /


fb(2,fa(3)); / *使用函数* /



这里是修复:


int f1(int);

void f2(int,int);


int(* fa)(int);

void(* fb)(int ,int);


fa = f1;

fb = f2;


/ *函数的类型指针真的没关系,我用void(*)

()* /

void(* fun_vec [])()= {fa,fb}; / *我认为不需要演员,但是

我不确定,任何人都可以提供帮助吗? * /


/ * ......稍后...... * /


fa = fun_vec [0];

fb = fun_vec [1];


I know that a variable (void *) is guaranteed to be able to store any
pointer and to allow conversion back to the original pointer.

I think, then, that I can store a pointer to a function in a (void *)
variable and then convert it back to the original type and use it as a
funtion.

The question is: am I correct or there might be situations where this
could fail?

To clarify, here is an example of what I''m trying to do:

/* Prototypes of functions that are defined somewhere */
int f1(int x);
void f2(int a, int b);

/* Variables of type "pointers to functions" */
int (*fa)(int);
void (*fb)(int, int);

/* An array of generic pointers */
void *fun_vec[2];

..... /* later in the code ... */
fun_vec[0] = f1; /* store pointers to f1 and f1 */
fun_vec[1] = f2; /* into void * */

..... /* even later ... */
fa = fun_vec[0]; /* get them back as pointers */
fb = fun_vec[1]; /* to functions */

fb(2,fa(3)); /* use the functions */

....

解决方案

Remo D. <rdwrote:

I know that a variable (void *) is guaranteed to be able to store any
pointer and to allow conversion back to the original pointer.

I think, then, that I can store a pointer to a function in a (void *)
variable and then convert it back to the original type and use it as a
funtion.

The question is: am I correct or there might be situations where this
could fail?

Unfortunately, function pointers aren''t covered by the C standard
as being convertible to a void pointer and back. There seem to be
some architectures where this leads to problems. On the other hand
in many cases you probably will be able to do so (e.g. on POSIX
compliant systems this has to work or e.g. the dlopen() couldn''t
be used).

To clarify, here is an example of what I''m trying to do:

/* Prototypes of functions that are defined somewhere */
int f1(int x);
void f2(int a, int b);

/* Variables of type "pointers to functions" */
int (*fa)(int);
void (*fb)(int, int);

/* An array of generic pointers */
void *fun_vec[2];

.... /* later in the code ... */
fun_vec[0] = f1; /* store pointers to f1 and f1 */
fun_vec[1] = f2; /* into void * */

.... /* even later ... */
fa = fun_vec[0]; /* get them back as pointers */
fb = fun_vec[1]; /* to functions */

fb(2,fa(3)); /* use the functions */

With most systems you should be fine doing it that way. Note that
you also can write the last e.g. as

fun_vec[ 1 ]( 2, fun_vec[ 0 ]( 3 ) );

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de


Jens Thoms Toerring ha scritto:

>
Unfortunately, function pointers aren''t covered by the C standard
as being convertible to a void pointer and back. There seem to be
some architectures where this leads to problems. On the other hand
in many cases you probably will be able to do so (e.g. on POSIX
compliant systems this has to work or e.g. the dlopen() couldn''t
be used).

Thanks Jens! I found a draft of the C standard (N869) on the internet
and, as you said, there''s no mention of pointers to functions being
convertible to void *.

However, in section 6.3.2.3 paragraph 8 they say:

>A pointer to a function of one type may be converted to
a pointer to a function of another type and back again;
the result shall compare equal to the original pointer.
If a converted pointer is used to call a function whose
type is not compatible with the pointed-to type, the
behavior is undefined.

So, if I would declare an array of function pointers, should it be safer?

/* array of 2 pointers to function returning int */
int (*fun_vec[2])();

Once I convert them to the appropriate function type, it should work (if
I read the standard correctly).

As you rightly pointed out it should also work as long as I use them
correctly (I would expect compiler warnings, though).

R:D


On Jun 19, 5:40 pm, "Remo D." <rdwrote:

I know that a variable (void *) is guaranteed to be able to store any
pointer and to allow conversion back to the original pointer.

It''s a common extension to do so, (see Annex G ISO 9899:1999) but
conforming programs should not rely on that.

I think, then, that I can store a pointer to a function in a (void *)
variable and then convert it back to the original type and use it as a
funtion.

Not allowed by ISO C.
You are, however, allowed to convert any function pointer to any other
function pointer and back, and get the same result.

The question is: am I correct or there might be situations where this
could fail?

Yes, for example if a function pointer is not really a pointer, or
cannot be represented by void *, etc.

To clarify, here is an example of what I''m trying to do:

/* Prototypes of functions that are defined somewhere */
int f1(int x);
void f2(int a, int b);

/* Variables of type "pointers to functions" */
int (*fa)(int);
void (*fb)(int, int);

/* An array of generic pointers */
void *fun_vec[2];

.... /* later in the code ... */
fun_vec[0] = f1; /* store pointers to f1 and f1 */
fun_vec[1] = f2; /* into void * */

.... /* even later ... */
fa = fun_vec[0]; /* get them back as pointers */
fb = fun_vec[1]; /* to functions */

fb(2,fa(3)); /* use the functions */

Here''s the fix:

int f1(int);
void f2(int, int);

int (*fa)(int);
void (*fb)(int, int);

fa = f1;
fb = f2;

/* the type of function pointer really doesn''t matter, I use void (*)
() */
void (*fun_vec[])() = { fa, fb }; /* I think a cast is not needed, but
I''m not sure, anyone can help? */

/* ... later ... */

fa = fun_vec[0];
fb = fun_vec[1];


这篇关于指向功能的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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