开发者

* glibc detected * realloc(): invalid next size: [duplicate]

This question already has answers here: Facing an error "*** glibc detected *** free(): invalid next size (fast)" (2 answers) Closed 8 years ago.

I have a problem with realloc function:

*** glibc detected *** realloc(): invalid next size:

Here is the relevant part of code:

    char* pathfile = NULL;
    int tcpargc=6;
    char *tcpargv[tcpargc];
    int it;
    for (it = 0;it < tcpargc;it++)
       tcpargv[it] = NULL;
    ...
    while (1) {
    ...
    if (pat开发者_如何学Gohfile == NULL)
       pathfile=(char*)malloc((strlen(RAMDIR)+strlen(tempfilename)+7)*sizeof(char));
    else {
    if ((tmp=(char*)realloc(pathfile,(strlen(RAMDIR)+strlen(tempfilename)+7)*sizeof(char))) == NULL)
    {
 printf("ERROR: realloc failed");
        free(pathfile);
    }
    else
        pathfile = tmp;
    }
    ...
    if (tcpargv[4] == NULL)
    tcpargv[4]=(char*)malloc((strlen("--infile=")+strlen(pathfile)+1)*sizeof(char));
    else {
    if ((tmp = (char*)realloc(tcpargv[4],strlen("--infile=")+strlen(pathfile)+1)*sizeof(char)))   == NULL){
    printf("ERROR: realloc failed");
    free(tcpargv[4]);
    }
    else
    tcpargv[4] = tmp;
    } 
    ...    
    }

I have checked it again and again but I could not find the error.

Thanks for you help.


That error indicates that your code is probably overwriting bookkeeping data used by the glibc memory allocation subsystem. As Michael Mior suggested in his comment, try running your code under Valgrind -- it's quite good at finding such memory corruption errors.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜