在C指针类型POSIX限制 [英] POSIX restrictions on pointer types in C
问题描述
背景
href=\"http://pubs.opengroup.org/onlinepubs/9699919799/nframe.html\"> POSIX标准增添了不少的库函数和其他标识符C语言的 则dlsym()
功能,它说(我强调):
概要
的#include<&dlfcn.h中GT;无效*对dlsym(void *的限制手柄,为const char *限制名);
说明
的则dlsym 的()函数将获得一个符号的地址
(一功能标识符或一个数据对象标识符)
...
块引用>C标准并不保证一个函数指针可以被转换为一个
空隙*
,或者甚至该指针的大小是相同的。这有效地增加了基于C的类型系统额外的限制。问题
我的问题是这样的:
- 有一个规范对于C的类型系统的这种限制引用,或者是从某些库功能的描述只是推断出?
- 是POSIX甚至在系统上实现的,其中
的sizeof(函数指针)>的sizeof(无效*)
?参考
- 的C11标准(最终公开草案): n1570
- POSIX标准从公开组:<一href=\"http://pubs.opengroup.org/onlinepubs/9699919799/nframe.html\">POSIX.1-2008
- 的POSIX <一个href=\"http://pubs.opengroup.org/onlinepubs/9699919799/functions/dlsym.html\"><$c$c>dlsym()$c$c>功能
解决方案借助对dlsym ()引用说,转换不是由C标准但符合标准的实现必须正确地做这项工作定义。那么在哪里这个不能做的工作不会是一个符合标准的实现,并将presumably文件系统这样的:
请注意,从一个void *指针转换函数指针为
在FPTR =(INT(*)(INT))的dlsym(手柄,my_function);
不是由ISO C标准定义。该标准要求这种
转换对符合实现正常工作。
块引用>有一个老一篇文章,这个从 C ++的角度谈并链接到一个旧版本的的dlsym()引用并有更详细的解释:
ISO C标准并不要求函数指针可投来回数据指针。事实上,ISO C标准
不需要void *类型的对象可以容纳一个指针
功能。实现支持XSI扩展,但是,这样做
要求void *类型的对象可以容纳一个指针
功能。一个指针转换为函数到的结果
指向另一个数据类型(除无效*)仍然是不确定的,
然而。需要注意的是编译器符合ISO C标准的
生成一个警告,如果需要从一个void *指针转换
一个函数指针试图为:FPTR =(INT(*)(INT))的dlsym(手柄,my_function);
由于这里要注意的问题,将来的版本可能需要添加新的
函数返回函数指针,或当前接口可以是
德$ P $赞成两个新功能pcated:一个是返回数据
指针和返回函数指针其他。
块引用>Background
The POSIX standard adds a lot of library functions and other identifiers to the C language. In the description of the
dlsym()
function, it says (with my emphasis):SYNOPSIS
#include <dlfcn.h> void *dlsym(void *restrict handle, const char *restrict name);
DESCRIPTION
The dlsym() function shall obtain the address of a symbol (a function identifier or a data object identifier) ...
The C standard doesn't guarantee that a function pointer can be converted to a
void *
, or even that the size of the pointers are the same. This effectively adds an additional restriction on C's type system.Question
My question is this:
- Is there a normative reference for this restriction of C's type system, or is it only deducible from the description of certain library functions?
- Is POSIX even implementable on a system where
sizeof (function pointer) > sizeof (void *)
?References
- The C11 Standard (final public draft): n1570
- The POSIX Standard from The Open Group: POSIX.1-2008
- The POSIX
dlsym()
function解决方案The dlsym() reference says the conversion is not defined by the C standard but that a conforming implementation has to make this work correctly. So on systems where this can not be made to work would not be a conforming implementation and would presumably document this:
Note that conversion from a void * pointer to a function pointer as in:
fptr = (int (*)(int))dlsym(handle, "my_function");
is not defined by the ISO C standard. This standard requires this conversion to work correctly on conforming implementations.
there is an old article that talks about this from the C++ perspective and links to an older version of the dlsym() reference and has a more detailed explanation:
The ISO C standard does not require that pointers to functions can be cast back and forth to pointers to data. Indeed, the ISO C standard does not require that an object of type void * can hold a pointer to a function. Implementations supporting the XSI extension, however, do require that an object of type void * can hold a pointer to a function. The result of converting a pointer to a function into a pointer to another data type (except void *) is still undefined, however. Note that compilers conforming to the ISO C standard are required to generate a warning if a conversion from a void * pointer to a function pointer is attempted as in:
fptr = (int (*)(int))dlsym(handle, "my_function");
Due to the problem noted here, a future version may either add a new function to return function pointers, or the current interface may be deprecated in favor of two new functions: one that returns data pointers and the other that returns function pointers.
这篇关于在C指针类型POSIX限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!