Really straight forward, but I'm trying to build a shared object and I'm testing it as I'm writing it (with a test exectuable I built using dlopen()). I've gotten to where I can print something from the shared object but when I pass an array I want modified it misbehaves.
Here is the shared object's source code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <libconfig.h>
#include <sys/stat.h>
#include <sys/acl.h>
#include <dlfcn.h>
#include <string.h>
#include <libgen.h>
#include <grp.h>
#include <unistd.h>
int** scratch_modArr ( int ** givenList ){
int numgroups=3;
*givenList=(int*)malloc( numgroups * sizeof(int) * 6 );
**givenList=500;
printf("modArr Ping: %d\n", (*givenList)[0]);
return givenList;
}
void scratch_ping(){
printf("main ping\n");
}
And the test executable's code:
Code:
#include <stdio.h>
#include <dlfcn.h>
int main (){
void *(*scratch_ping)();
int **(*scratch_modArr)();
void *libHandle;
int newArr[1]= {1};
libHandle=dlopen("./libscratch.so", RTLD_LAZY);
scratch_ping=dlsym(libHandle, "scratch_ping");
scratch_modArr=dlsym(libHandle, "scratch_modArr");
printf("Before Call: %d\n", newArr[0]);
(*scratch_ping)();
(*scratch_modArr)(newArr);
printf("After Call: %d\n", newArr[0]);
return 0;
}
This is the executable's command line output:
Code:
[root@thatch scratch]# ./scratch-test
Before Call: 1
main ping
modArr Ping: 500
After Call: 7986464
[root@thatch scratch]#
Obviously, something's gone awry. My assumption was that malloc gave me a new segment of memory which killed off my old pointer. But I'm not sure how to modify the return type on the functions so that it can do that.