在以下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