开发者

External Functions and Parameter Size Limitation (C)

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

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 ...
    asprintf(&query, "select mid, rating "
                     "from %s "
                     "where uid=%d "
                     "order by rand()", itable, uid);
    if (mysql_query(conn2, query)) {
        fprintf(stderr, "%s\n", mysql_error(conn2));
        exit(1);
    }
    res2 = mysql_store_result(conn2);
    int movieCount = mysql_num_rows(res2);
    // withhold is 开发者_如何学Goa variable that defines a percentage of the entries 
    // to be used for calculations (generally 20%)
    int listSize = round((movieCount * ((double)withhold/100)));
    struct ratingObj moviesToRate[listSize];
    int mvCount = 0;
    int count =0;
    while ((row2 = mysql_fetch_row(res2)) != NULL){
        if(count<(movieCount-listSize)){
            // adds to another table
        }else{
            moviesToRate[mvCount].uid = uid;
            moviesToRate[mvCount].mid = atoi(row2[0]);
            moviesToRate[mvCount].rating = 0.0;

            mvCount++;
        }
        count++;
    }
    // ... more code ...
    FunctionLib = dlopen("library.so", RTLD_LAZY);
    dlError = dlerror();
    if( dlError ) exit(1);
    Function = dlsym( FunctionLib, "getResults");
    dlError = dlerror();
    (*Function)( moviesToRate, listSize );
// .. more code
}

library.c

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
}


You are likely blowing up the stack. Move the array to outside of the function, i.e. from auto to static land.

Another option is that the // ... more code - array gets populated... part is corrupting the stack.

Edit 0:

After you posted more code - you are using C99 variable sized array on the stack - Bad IdeaTM. Think what happens when your data set grows to thousands, or millions, of records. Switch to dynamic memory allocation, see malloc(3).


You don't show us what listsize is, but I suppose it is a variable and not a constant.

What you are using are variable length arrays, VLA. These are a bit dangerous if they are too large since they usually allocated on the stack.

To work around that you can allocate such a beast dynamically

struct ratingObj (*movies)[listSize] = malloc(sizeof(*movies));
// ...
free(movies);

You'd then have in mind though that movies then is a pointer to array, so you have to reference with one * more than before.

Another, more classical C version would be

struct ratingObj * movies = malloc(sizeof(*movies)*listsize);
// ...
free(movies);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜