Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dlsym() workaround return type

Tags:

c++

dlsym

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 *.

like image 584
caprice-e Avatar asked Feb 04 '26 17:02

caprice-e


1 Answers

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.

like image 72
cdhowie Avatar answered Feb 07 '26 10:02

cdhowie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!