将指向函数指针的指针存储在 void* 中
Storing the pointer to a function pointer in a void*
我明白你为什么做不到:
void(*fp)(void) = &function;
function_taking_void_pointer((void*)fp);
因为类型的长度可能不同。
但是添加另一个间接层有什么问题吗:
void(*fp)(void) = &function;
void(**fpp)(void) = &fp;
function_taking_void_pointer((void*)fpp)
我的想法:指向函数指针的指针应该指向数据内存,因此应该与 void* 类型具有相同的长度。
那我到底错在哪里了?
这段代码是错误的。
void(*fp)(void) = &function;
function_taking_void_pointer((void*)fp);
在 C/C++ 中,function name
将被重新定位到 linking stage
中的特定地址,因此您不能在 function name
之前使用 &
。另外function name
不是C/C++中的变量,所以你也不能尝试获取它的地址。
你是对的,所有指针类型都是对象类型:
N1570 6.3.5 类型,第 20 段,第五个列表项:
- A pointer type may be derived from a function type or an object type, called the
referenced type. A pointer type describes an object whose value provides a reference
to an entity of the referenced type. A pointer type derived from the referenced type T
is sometimes called ‘‘pointer to T’’. The construction of a pointer type from a
referenced type is called ‘‘pointer type derivation’’. A pointer type is a complete
object type.
但是指向对象类型的指针不一定具有与 void*
(6.2.5 p28) 相同的大小。
- A pointer to void shall have the same representation and alignment requirements as a
pointer to a character type.48) Similarly, pointers to qualified or unqualified versions of
compatible types shall have the same representation and alignment requirements. All
pointers to structure types shall have the same representation and alignment requirements
as each other. All pointers to union types shall have the same representation and
alignment requirements as each other. Pointers to other types need not have the same
representation or alignment requirements.
但是,它们都可以转换为void*
(6.3.2.3 p1):
- A pointer to void may be converted to or from a pointer to any object type. A pointer to
any object type may be converted to a pointer to void and back again; the result shall
compare equal to the original pointer.
我明白你为什么做不到:
void(*fp)(void) = &function;
function_taking_void_pointer((void*)fp);
因为类型的长度可能不同。
但是添加另一个间接层有什么问题吗:
void(*fp)(void) = &function;
void(**fpp)(void) = &fp;
function_taking_void_pointer((void*)fpp)
我的想法:指向函数指针的指针应该指向数据内存,因此应该与 void* 类型具有相同的长度。
那我到底错在哪里了?
这段代码是错误的。
void(*fp)(void) = &function; function_taking_void_pointer((void*)fp);
在 C/C++ 中,function name
将被重新定位到 linking stage
中的特定地址,因此您不能在 function name
之前使用 &
。另外function name
不是C/C++中的变量,所以你也不能尝试获取它的地址。
你是对的,所有指针类型都是对象类型:
N1570 6.3.5 类型,第 20 段,第五个列表项:
- A pointer type may be derived from a function type or an object type, called the referenced type. A pointer type describes an object whose value provides a reference to an entity of the referenced type. A pointer type derived from the referenced type T is sometimes called ‘‘pointer to T’’. The construction of a pointer type from a referenced type is called ‘‘pointer type derivation’’. A pointer type is a complete object type.
但是指向对象类型的指针不一定具有与 void*
(6.2.5 p28) 相同的大小。
- A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.48) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.
但是,它们都可以转换为void*
(6.3.2.3 p1):
- A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.