dlsym() workaround return type

六月ゝ 毕业季﹏ 提交于 2019-12-20 03:32:43

问题


The man page of dlsym() lists

   *(void **) (&cosine) = dlsym(handle, "cos");

as a workaround for casting the return value of dlsym().

What's the meaning of *(void **) (&cosine) here? I understand cosine is a function pointer defined previously, but I'm not sure why an ampersand & is needed before the name (an error without &). Moreover, I don't figure out why the pointer of void * (void **) is again used with *.


回答1:


Let's unwrap it a bit at a time:

&cosine

This takes a pointer to the variable cosine, so this will be a pointer to a function pointer.

(void **) &cosine

We cast the pointer-to-function-pointer to pointer-to-pointer-to-void.

* (void **) &cosine

We dereference the casted pointer, assigning the result of dlsym() into it.

Effectively, what's happening is a side-step of the issue. Instead of casting the result of dlsym() into the correct type of function pointer, we pretend that cosine is a void * (through a level of indirection) and assign to it.



来源:https://stackoverflow.com/questions/43509159/dlsym-workaround-return-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!