简体   繁体   English

这有什么意义:*(void **)(&fptr)= dlsym(handle,“my_function”);`

[英]How does this make sense: *(void **)(&fptr) = dlsym(handle, “my_function”);`

The code comes from this page: http://pubs.opengroup.org/onlinepubs/009695399/functions/dlsym.html 代码来自此页面: http//pubs.opengroup.org/onlinepubs/009695399/functions/dlsym.html

Can you help me understand this? 你能帮我理解一下吗? It takes the address of the function pointer, casts it to void** and then dereferences it. 它接受函数指针的地址,将其转换为void **然后取消引用它。 I don't know why it has to work like this. 我不知道为什么它必须像这样工作。

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". 到目前为止,我所得到的唯一建议是“从右到左阅读”或类似“从右到左阅读”。

The meaning of the code is: 代码的含义是:

  1. Take the address of fptr . 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". 将指针表达式转换为“指向void的指针”。
  3. Dereference that pointer to access the object fptr as if it were an object of type void * . 取消引用访问对象fptr指针,就好像它是void *类型的对象一样。
  4. Assign the result of the right to the lvalue obtained in step 3. 将权限的结果分配给在步骤3中获得的左值。

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. 不幸的是,在POSIX中编写此示例的人都处于破解状态,因为步骤3违反了C语言的别名规则,从而调用了未定义的行为。 In particular, real-world compilers will optimize this code in ways that breaks the intended usage. 特别是, 真实编译器将以破坏预期用途的方式优化此代码。

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 . 这个例子的作者试图实现的是避免将指针指向void的右侧转换指向函数的指针 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. 这是基于C标准要求此演员生成警告的声明,但我已经彻底搜索了这样的要求,并且没有找到这样的要求。

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: 如果确实存在这样的问题(警告要求),那么在不调用未定义行为的情况下使警告静音的唯一方法(如POSIX文本中的错误示例)就是这样做:

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

The function returns an a function pointer. 该函数返回一个函数指针。 That code says hey take my function pointer variable give me the address of it. 该代码说嘿接受我的函数指针变量给我的地址。 Cast it to void ** . 把它变成void ** Now deference the void ** and set the value of the void * = to the pointer I got from the call. 现在请参考void **并将void * =的值设置为我从调用中获得的指针。

*(void **)(&fptr) = dlsym(handle, “my_function”);

To make this simpler, 为了使这更简单,

fptr is a pointer. fptr是一个指针。

&fptr is the address of that pointer. &fptr是该指针的地址。

you are typecasting it to a pointer to a pointer to a void 你将它类型转换为pointer to a pointer to a void

and then reference and assign the return value from function. 然后引用并分配函数的返回值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM