Thread: arrays, structs, dlls, functions...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    arrays, structs, dlls, functions...

    Hi, I am very much stuck in the following issue. Any help is very much appreciated!

    Basically I have a program wich contains an array of structs and I am getting a segmentation error when I call an external function. The error only happens when I have more than 170 items on the array being passed.

    Nothing on the function is processed. The program stops exactly when accessing the function.

    Is there a limit for the size of the parameters that are passed to external functions?

    Main.c
    Code:
    struct ratingObj {
    	int uid;
    	int mid;
    	double rating;
    };
    
    void  *FunctionLib;		/*  Handle to shared lib file	*/
    void (*Function)();		/*  Pointer to loaded routine	*/
    const char *dlError;		/*  Pointer to error string	*/
    
    int main( int argc, char * argv[]){
    // ... some code ...
    	struct ratingObj movies[listSize];
    // ... more code ...
    	FunctionLib = dlopen("dll.so", RTLD_LAZY);
    	dlError = dlerror();
    	if( dlError ) exit(1);
    	Function = dlsym( FunctionLib, "getResults");
    	dlError = dlerror();
    	(*Function)( moviesToRate, listSize );
    // .. more code
    }
    library.c
    Code:
    struct ratingObj {
    	int uid;
    	int mid;
    	double rating;
    };
    
    typedef struct ratingObj ratingObj;
    
    void getResults(struct ratingObj *moviesToRate, int listSize);
    
    void getResults(struct ratingObj *moviesToRate, int listSize){
    // ... more code
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    It's hard to say for sure without seeing the code. At the very least, you should declare the parameter types in your function pointer:
    Code:
    void (*Function)(struct ratingObj *, int);
    The more type safety, the better.
    Is there a limit for the size of the parameters that are passed to external functions?
    There's no special limit beyond what limits you'd normally have in calling a function; but you're passing the same size objects no matter how many items there are. Your pointer will always be the same size, as will your int.

    Have you tried running the program under a debugger? That should be your very first move when you have a segfault.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Talking

    Thanks cas. Attached the gdb debugger and found several issues. I converted all the main arrays to use malloc and now the code is more reliable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with creating arrays outiside functions
    By kaylors in forum C++ Programming
    Replies: 1
    Last Post: 10-28-2009, 11:04 AM
  2. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 09:41 PM
  3. functions and arrays
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-14-2002, 09:57 AM
  4. 2D arrays with functions made easy
    By goran in forum C Programming
    Replies: 1
    Last Post: 09-17-2001, 12:08 PM
  5. elements of arrays; functions
    By sballew in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 01:48 AM