Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Where does the line</p> <pre><code>f = reinterpret_cast&lt;void(*)(int*)&gt;(dlsym(RTLD_DEFAULT, "real_f.symbol_name_")); </code></pre> <p>occur in <code>test.cc</code>? The pointer will be initialized when the line is executed (which of course depends on when the function which contains it is called). Or did you mean to write</p> <pre><code>void (*f)(int* ) = reinterpret_cast&lt;void(*)(int*&gt;(dlsym(RTLD_DEFAULT, "real_f.symbol_name_"); </code></pre> <p>? In this case, the pointer will be initialized during static initialization. Which means that you still have order of initialization issues if you try to use the pointers in the constructor of a static object.</p> <p>The classical solution for this would be to use some sort of singleton:</p> <pre><code>struct LibraryPointers { void (*f)(int* ); // ... static LibraryPointers const&amp; instance() private: LibraryPointers(); }; LibraryPointers const&amp; LibraryPointers::instance() { static LibraryPointers theOneAndOnly; return theOneAndOnly; } LibraryPointers::LibraryPointers() : f( reinterpret_cast&lt;void(*)(int*)&gt;(dlsym(RTLD_DEFAULT, "real_f.symbol_name_")) ) , // initialization of other pointers... { } </code></pre> <p>Then wrap the library in a C++ class which uses this structure to get the addresses of the pointers.</p> <p>And one last remark: the <code>reinterpret_cast</code> you are trying to do isn't legal, at least not formally. (I think that both Sun CC and g++ will accept it, however.) According to Posix, the correct way to get a pointer to function from <code>dlsym</code> would be:</p> <pre><code>void (*f)(int* ); *reinterpret_cast&lt;void**&gt;(&amp;f) = dlsym(...); </code></pre> <p>This doesn't lend itself to initializations, however.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload