在以下C++
代碼,我試圖從共享庫加載函數。Segfault轉換指針與reinterpret_cast
void* tmp = dlsym(dl_lib, symbol);
_fun = reinterpret_cast<plot_sample_fun*>(tmp);
但是,在轉換指針時reinterpret_cast
段錯誤。這裏有什麼問題?
編輯:
爲了提供進一步的上下文中,
typedef void plot_sample_fun(const void *, double **, char ***, size_t *);
class Foo {
void bar(); // Loads _fun as above.
plot_sample_fun* _fun;
};
您可以閱讀http://stackoverflow.com/questions/310451/should-i-use-static-cast-or-reinterpret-cast-when-casting-a-void-to-whatever – chrizke 2014-11-20 19:02:42
什麼是_fun? reinterpret_cast不會自行執行例外。它只會迫使編譯器相信指針是某種類型的。但是,段錯誤是關於將指針存儲在其他一些內存位置。 – AlexanderVX 2014-11-20 19:06:34
它確實是segfaults的'reinterpret_cast',而不是以後使用的指針?爲什麼你不檢查它不是null? – 2014-11-20 19:09:18