我遇到了一個奇怪的問題。 我有兩個文件AC和BC如下: BC:無法調用返回指針的函數指針
#include <stdlib.h>
int *foo() {
int *x;
x = (int *) malloc(sizeof(int));
*x = 4;
return x;
}
我編譯BC使用gcc來b.so: $ gcc的-o b.so -shared -fpic
交流:
#include <stdio.h>
#include <dlfcn.h>
int main() {
void *hdl;
hdl = dlopen("./b.so", RTLD_LAZY);
int *((*fn)(void));
int *x;
x = (*fn)();
fn = dlsym(hdl, "foo");
printf("%d", *x);
}
我使用編譯AC GCC:
$ gcc的-fpic -ldl交流
現在,當我運行它:
$ ./a.out 分段故障
在那裏我我的問題呢? 這在b.c中的函數沒有返回指針時有效。
此外,我嘗試使用dlerror()檢查錯誤,但它沒有報告。
初始化(或分配一個值到它),而不是限定。變量'fn'在使用前定義。 – 2010-08-06 05:59:29
哦,謝謝,那是我相信的一個錯字。 現在,它的工作。 – Nilesh 2010-08-06 07:41:59
@Georg Fritzsche:Sie haben recht,danke。 – Beta 2010-08-06 17:37:10