请问这个是有意义的:*(无效**)(安培; FPTR)=的dlsym(手柄," my_function");` [英] How does this make sense: *(void **)(&fptr) = dlsym(handle, "my_function");`

查看:108
本文介绍了请问这个是有意义的:*(无效**)(安培; FPTR)=的dlsym(手柄," my_function");`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code来源于此页:的http://酒吧。 opengroup.org/onlinepubs/009695399/functions/dlsym.html

The code comes from this page: http://pubs.opengroup.org/onlinepubs/009695399/functions/dlsym.html

你能帮助我理解这一点?
它采用函数指针的地址,将其转换为void **,然后取消引用它。我不知道为什么它有这样的工作。

Can you help me understand this? It takes the address of the function pointer, casts it to void** and then dereferences it. I don't know why it has to work like this.

我AP preciate您的帮助!到现在为止,我已经得到了唯一的建议是什么样,在从右向左读的周期是从右至左读它。

I appreciate your help! Until now, the only advice I have gotten was "read it from right to left" or something like "read it in cycles from right to left".

推荐答案

在code的含义是:


  1. FPTR 的地址。类型此前pression的是指针到指针到函数(某些特定类型的)。​​

  2. 演员该指针前pression为指针指向void。

  3. 取消引用该指针访问该对象 FPTR 犹如键入的对象无效*

  4. 分配到在步骤3中得到的左值正确的结果。

  1. Take the address of fptr. The type of this expression is pointer-to-pointer-to-function (of some specific type).
  2. Cast that pointer expression to "pointer to pointer to void".
  3. Dereference that pointer to access the object fptr as if it were an object of type void *.
  4. Assign the result of the right to the lvalue obtained in step 3.

不幸的是,谁在POSIX写这个例子是裂纹,因为第3步违反了C语言的别名规则,从而调用未定义的行为。尤其是,真实世界编译器将在打破了使用目的的方法优化此code。

Unfortunately, whoever wrote this example in POSIX was on crack, because step 3 violates the aliasing rules of the C language and thus invokes undefined behavior. In particular, real-world compilers will optimize this code in ways that breaks the intended usage.

什么这个例子的作者是的尝试的实现是避免因的铸造右侧指向void 的到的函数指针的。这是基于对C标准要求这个转换生成一个警告声明,但我已经彻底搜查了这样的要求能及时发现没有这样的规定。

What the author of this example was trying to achieve was avoiding casting the right-hand side from pointer to void to pointer to function. This is based on a claim that the C standard requires this cast to generate a warning, but I have searched thoroughly for such a requirement and can find no such requirement.

如果这样的问题确实存在(警告要求),那么唯一的办法沉默警告,而不必调用未定义行为(如在POSIX的文本中的坏榜样)是这样做:

If such a problem really does exist (the warning requirement), then the only way to silence the warning without invoking undefined behavior (like the bad example in the text of POSIX) is to do this:

void (*fptr)(); // or whatever function pointer type you want
void *temp = dlsym(handle, "my_function");
memcpy(&fptr, &temp, sizeof fptr);

这篇关于请问这个是有意义的:*(无效**)(安培; FPTR)=的dlsym(手柄," my_function");`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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