clumsy cast in dlopen.3

Bruno Haible <[email protected]>
Newsgroups org.kernel.vger.linux-man
Organization GNU
Message-ID <21436742.Yz81rIOvuz@nimes>
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?

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

Bruno
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.