Hi there, I'm stuck with a weird problem. I have two files a.c and b.c as follows: b.c:
#include <stdlib.h>
int *foo() {
int *x;
x = (int *) malloc(sizeof(int));
*x = 4;
return x;
}
I compile b.c to b.so using gcc: $ gcc -o b.so -shared -fpic
a.c:
#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);
}
I compile a.c using gcc:
$ gcc -fpic -ldl a.c
Now when I run it:
$ ./a.out Segmentation fault
Where I'm I going wrong? This works when the function in b.c doesn't return a pointer.
And moreover, I tried checking for errors using dlerror(), but it reports none.