【发布时间】:2010-08-06 05:52:09
【问题描述】:
我遇到了一个奇怪的问题。 我有两个文件 a.c 和 b.c 如下: 公元前:
#include <stdlib.h>
int *foo() {
int *x;
x = (int *) malloc(sizeof(int));
*x = 4;
return x;
}
我使用 gcc 将 b.c 编译为 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);
}
我使用 gcc 编译 a.c:
$ gcc -fpic -ldl a.c
现在当我运行它时:
$ ./a.out 分段错误
我哪里错了? 这在 b.c 中的函数不返回指针时有效。
此外,我尝试使用 dlerror() 检查错误,但它没有报告任何错误。
【问题讨论】:
标签: c pointers function-pointers