0

我对 realloc 函数有疑问:

*** 检测到 glibc *** realloc():下一个大小无效:

这是代码的相关部分:

    char* pathfile = NULL;
    int tcpargc=6;
    char *tcpargv[tcpargc];
    int it;
    for (it = 0;it < tcpargc;it++)
       tcpargv[it] = NULL;
    ...
    while (1) {
    ...
    if (pathfile == 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;
    } 
    ...    
    }

我一遍又一遍地检查它,但我找不到错误。
谢谢你的帮助。

4

1 回答 1

1

该错误表明您的代码可能正在覆盖 glibc 内存分配子系统使用的簿记数据。正如 Michael Mior 在他的评论中建议的那样,尝试在Valgrind下运行您的代码——它非常适合发现此类内存损坏错误。

于 2010-09-21T18:05:34.323 回答