将函数指针转换为void * [英] cast function pointer to void*

查看:106
本文介绍了将函数指针转换为void *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

*希望这个帖子不违反这个组的规则*


我在GNOME的glib-2.12源代码中找到了以下代码:


/ *在gclosure.c * /

GClosure *

g_cclosure_new(GCallback callback_func,

gpointer user_data ,

GClosureNotify destroy_data)

{

......

/ *((GCClosure *)闭包) - >回调类型为gpointer * /

((GCClosure *)闭包) - > callback =(gpointer)callback_func;

}


这里将GCallback转换为gpointer类型。

GCallback和gpointer的定义如下:

typedef void(* GCallback)(void); / * in gclosure.h * /

typedef void * gpointer; / *在gtypes.h * /


因此结果是a(void(*)(void))被强制转换为(void *)。然而,对这个组和c99标准的搜索
表示向void指针投射

函数指针是未定义的行为。我想知道

是否更适合滑动练习。

解决方案

WaterWalk写道:
< blockquote class =post_quotes>
*希望这个帖子不违反这个组的规则*


我在GNOME'中发现了以下代码glib-2.12来源:

[...]

因此结果是a(void(*)(void))被强制转换为(void *)。然而,对这个组和c99标准的搜索
表示向void指针投射

函数指针是未定义的行为。



这是违反约束的行为,实际上意味着它可能无法编译,或者

如果编译,/整个/程序超出了

C标准的范围(即使转换实际上从未执行过)。


我想知道是否

滑稽练习更可取。



优先购买什么?在标准C中根本没有办法可靠地将
转换为对象指针的函数指针,所以如果有一个

这样做的正当理由(我不是知道这里是否有充分的理由

),必须使用特定于实现的扩展来完成。


" ; WaterWalk" < to ******** @ 163.comwrote:


*希望这个帖子不违反这个组的规则*



代码是偏离主题的,但你问的问题 - 基本上,

_is_这个ISO C - 是关于主题的。


/ *在gclosure.c * /

GClosure *

g_cclosure_new(GCallback callback_func,

gpointer user_data,

GClosureNotify destroy_data)

{

......

/ *((GCClosure *)闭包) - >回调类型为gpointer * /

((GCClosure *)闭包) - > callback =(gpointer)callback_func;

}


这里将GCallback转换为gpointer类型。

GCallback和gpointer的定义如下:

typedef void(* GCallback)(void); / * in gclosure.h * /

typedef void * gpointer; / *在gtypes.h * /


因此结果是a(void(*)(void))被强制转换为(void *)。然而,对这个组和c99标准的搜索
表示向void指针投射

函数指针是未定义的行为。我想知道

滑稽练习是否更可取。



是的,它是未定义的行为;因此,不,这是不可取的。

也没有必要,因为ISO C要求所有功能指针

类型可以相互转换没有损失

信息。


Richard


Harald van D?3k写道:


WaterWalk写道:


*希望这个帖子不违反这个组的规则*


我在GNOME的glib-2.12源代码中找到了以下代码:

[...]

所以结果是a(void(*)(void))被强制转换为(void *)。然而,对这个组和c99标准的搜索
表示向void指针投射

函数指针是未定义的行为。



这是违反约束的行为,实际上意味着它可能无法编译,或者如果它编译,则为/ b $ b,/整个/程序超出了

C标准的范围(即使转换永远不会被执行)。



实际上......我知道它被多个

编译器视为违反约束,但我似乎无法理解找到标准中的任何措辞

这样说。如果有人能说出来,我会很感激。


*Hope this thread doesn''t violate this group''s rule*

I found the following code in the GNOME''s glib-2.12 source:

/* In gclosure.c */
GClosure*
g_cclosure_new (GCallback callback_func,
gpointer user_data,
GClosureNotify destroy_data)
{
......
/* ((GCClosure*) closure)->callback is of type gpointer */
((GCClosure*) closure)->callback = (gpointer) callback_func;
}

Here a GCallback is cast to a gpointer type. The definition of
GCallback and gpointer are as follows:
typedef void (*GCallback) (void); /* in gclosure.h */
typedef void* gpointer; /* in gtypes.h */

So the result is that a (void (*)(void)) is cast to (void*). However a
search of this group and c99 standard indicates that casting a
function pointer to a void pointer is undefined behavior. I wonder if
the glib practice is preferable.

解决方案

WaterWalk wrote:

*Hope this thread doesn''t violate this group''s rule*

I found the following code in the GNOME''s glib-2.12 source:
[...]
So the result is that a (void (*)(void)) is cast to (void*). However a
search of this group and c99 standard indicates that casting a
function pointer to a void pointer is undefined behavior.

It''s a constraint violation, actually, meaning it might not compile, or
if it does compile, the /entire/ program is outside of the scope of the
C standard (even if the conversion would never actually be performed).

I wonder if
the glib practice is preferable.

Preferable to what? There''s simply no way in standard C to reliably
convert function pointers to object pointers, so if there is a
legitimate reason to do so (I don''t know if there is a good reason
here), it must be done using an implementation-specific extension.


"WaterWalk" <to********@163.comwrote:

*Hope this thread doesn''t violate this group''s rule*

The code is off-topic, but the question you ask about it - basically,
_is_ this ISO C - is on-topic.

/* In gclosure.c */
GClosure*
g_cclosure_new (GCallback callback_func,
gpointer user_data,
GClosureNotify destroy_data)
{
......
/* ((GCClosure*) closure)->callback is of type gpointer */
((GCClosure*) closure)->callback = (gpointer) callback_func;
}

Here a GCallback is cast to a gpointer type. The definition of
GCallback and gpointer are as follows:
typedef void (*GCallback) (void); /* in gclosure.h */
typedef void* gpointer; /* in gtypes.h */

So the result is that a (void (*)(void)) is cast to (void*). However a
search of this group and c99 standard indicates that casting a
function pointer to a void pointer is undefined behavior. I wonder if
the glib practice is preferable.

Yes, it''s undefined behaviour; and therefore, no, it''s not preferable.
Nor is it necessary, since ISO C requires that all function pointer
types can be converted to and from one another without loss of
information.

Richard


Harald van D?3k wrote:

WaterWalk wrote:

*Hope this thread doesn''t violate this group''s rule*

I found the following code in the GNOME''s glib-2.12 source:
[...]
So the result is that a (void (*)(void)) is cast to (void*). However a
search of this group and c99 standard indicates that casting a
function pointer to a void pointer is undefined behavior.


It''s a constraint violation, actually, meaning it might not compile, or
if it does compile, the /entire/ program is outside of the scope of the
C standard (even if the conversion would never actually be performed).

Actually... I know it''s treated as a constraint violation by multiple
compilers, but I can''t seem to find any wording in the standard that
says so. I''d appreciate it if anyone could point it out.


这篇关于将函数指针转换为void *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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