Friday, August 25, 2006

Pointer to Function vs Pointer to Data

From http://www.opengroup.org/onlinepubs/009695399/functions/dlsym.html:

--- start ---

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");
--- end ---

Hmm...

Hmm...

Hmm...

So that's why they do it like this:


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

me, on learning how to load a library ...

and the really funny thing:
I was actually searching first to linux kernel code to learn how they did module load/unload, before resorted to dl*() family of functions...

Poor me.

No comments: