AW: clumsy cast in dlopen.3
Walter Harms <[email protected]>
| Newsgroups | org.kernel.vger.linux-man |
|---|---|
| Message-ID | <[email protected]> |
Hello, I agree the cast is not nice, (someone for a extension of C standard ?) but i have to admit that i have never seen the trick with the union. But it needs some explaination. The comment in the example is already huge, i would ask for a comment subsektion for this behavier here. btw: the original code in the example looks like this ... cosine = (typeof(double (double)) *) dlsym(handle, "cos"); my2c wh ________________________________________ Von: Alejandro Colomar <[email protected]> Gesendet: Donnerstag, 14. Mai 2026 13:29:20 An: Bruno Haible Cc: [email protected]; Martin Uecker Betreff: Re: clumsy cast in dlopen.3 Hi Bruno, On 2026-05-14T12:56:55+0200, Bruno Haible wrote: > The dlopen.3 man page contains this text: > > *(void **) &cosine = dlsym(handle, "cos"); > > This (clumsy) cast conforms with the ISO C standard and will > avoid any compiler warnings. > > However, such a cast violates the strict aliasing rules of ISO C, no? I think I agree. Dereferencing the pointer &cosine with a type different than the type of the object is not allowed. I've CCed Martin, who might be able to confirm. > > The proper workaround is to use a union: > > union { double (*cosine) (double); void *pointer; } u; > > u.pointer = dlsym(handle, "cos"); > ... > printf("%f\n", u.cosine(2.0)); This is seems much better, indeed. Have a lovely day! Alex > > Bruno > > > > -- <https://www.alejandro-colomar.es>