且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将函数指针作为字符数组传递

更新时间:2023-11-11 11:56:28

Olaf Dietrich写道:

我可能只是暂时(希望......)愚蠢,但是怎么能

我使用

(有符号/无符号)字符数组(以符合标准的方式)传递函数之间的函数指针?


例如:


我有一个函数:int(* func)(double,int)。


现在我我希望存储地址。这个函数是一个数组(足够大小)的字符,例如
,例如在

char func_addr [100]。 (我怎么能这样做?)



char func_addr [sizeof func];

memcpy(func_addr,& func,sizeof func) ;


最后我想在另一个

函数指针中使用这个地址:

new_func =( int(*)(double,int))func_addr。



memcpy(& new_func,func_addr,sizeof new_func);


这不起作用 - gcc抱怨:ISO C禁止转换

指向函数指针类型的对象指针。



那是因为你试图转换func_addr,在这种情况下

衰变为指向第一个char的指针那个数组,成为指向

a函数的指针。你想要它做的是解释那个

数组的内容,好像它是一个指向函数的指针。这种

转换的语法(安全)是:


new_func = *(int(**)(double,int))func_addr ;


这将func_addr从指向char数组的指针转换为

指向函数指针的指针,然后取消引用该指针,获取

a函数指针。不幸的是,这不是一个安全的方法来使用,因为不能保证func_addr正确对齐

持有这样的指针。如果func_addr

是指向动态分配的char数组的指针,或者如果func_addr

是具有相应函数指针类型的并集的一部分,则可以保证正确对齐。


James Kuyper< ja ********* @ verizon.net>:

Olaf Dietrich写道:

>现在我想存储地址。这个函数在一个数组(足够大小)的字符中,例如,在
char func_addr [100]。 (我怎么能这样做?)



char func_addr [sizeof func];

memcpy(func_addr,& func,sizeof func) ;


>最后我想在另一个
函数指针中使用这个地址:



memcpy(& new_func,func_addr,sizeof new_func);



多么明显和简单......(甚至可以工作)。


非常感谢您的帮助

Olaf


10月22日13:10,o ... @ dtrx.de(Olaf Dietrich)写道:

我可能只是暂时(希望......)愚蠢,但怎么能

我使用数组
传递函数之间的函数指针
(签名/未签名)字符(符合标准的方式)?



你为什么要这样做?某种回调功能?

如果可以,***避免使用。


< snip>


-

Nick Keighley


I may just be temporarily (hopefully ...) stupid, but how can
I pass a function pointer between functions using an array of
(signed/unsigned) chars (in a standard-conforming way)?
E.g.:

I have a function: int (*func)(double, int).

Now I''d like to store the "address" of this function
in an array (of sufficient size) of chars, e.g. in
char func_addr[100]. (How can I do this?)

And finally I would like to use this address in another
function pointer:
new_func = (int (*)(double, int)) func_addr.

This doesn''t work - gcc complains: "ISO C forbids conversion
of object pointer to function pointer type".
Is there a way to store the data that constitutes the function
pointer in an array of chars (that need not have the size of
an object pointer)? And then to re-interpret this sequence of
bytes again as a function pointer?
Thanks for any suggestions
Olaf

Olaf Dietrich wrote:
I may just be temporarily (hopefully ...) stupid, but how can
I pass a function pointer between functions using an array of
(signed/unsigned) chars (in a standard-conforming way)?
E.g.:

I have a function: int (*func)(double, int).

Now I''d like to store the "address" of this function
in an array (of sufficient size) of chars, e.g. in
char func_addr[100]. (How can I do this?)


char func_addr[sizeof func];
memcpy(func_addr, &func, sizeof func);

And finally I would like to use this address in another
function pointer:
new_func = (int (*)(double, int)) func_addr.

memcpy(&new_func, func_addr, sizeof new_func);

This doesn''t work - gcc complains: "ISO C forbids conversion
of object pointer to function pointer type".

That''s because you''re trying to convert func_addr, which in this context
decays into a pointer to the first char of that array, into a pointer to
a function. What you want it to do is interpret the contents of that
array as if it were a pointer to a function. The syntax for such a
conversion (were it safe) would be:

new_func = *(int (**)(double, int)) func_addr;

This converts func_addr from a pointer to an array of char into a
pointer to a function pointer, then dereferences that pointer, obtaining
a function pointer. This is not, unfortunately, a safe approach to
use, because there''s no guarantee that func_addr is correctly aligned to
hold such a pointer. You could guarantee correct alignment if func_addr
were a pointer to a dynamically allocated array of char, or if func_addr
were part of a union with a the appropriate function pointer type.


James Kuyper <ja*********@verizon.net>:
Olaf Dietrich wrote:
>Now I''d like to store the "address" of this function
in an array (of sufficient size) of chars, e.g. in
char func_addr[100]. (How can I do this?)


char func_addr[sizeof func];
memcpy(func_addr, &func, sizeof func);

>And finally I would like to use this address in another
function pointer:


memcpy(&new_func, func_addr, sizeof new_func);

How obvious and simple ... (and it even works).

Thank you very much for your help
Olaf


On 22 Oct, 13:10, o...@dtrx.de (Olaf Dietrich) wrote:
I may just be temporarily (hopefully ...) stupid, but how can
I pass a function pointer between functions using an array of
(signed/unsigned) chars (in a standard-conforming way)?

why do you want to do this? Some sort of callback function?
It''s best avoided if you can.

<snip>

--
Nick Keighley