6

我对 C 很陌生,似乎无法弄清楚以下代码有什么问题。

int main() {
    char filen[] = "file.txt";
    FILE *file = fopen ( filen, "r" );
    if ( file != NULL )
    {
        char line [ 128 ];
        while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
        {
            int i;
            char *result;
            for(i=0; i< NUM;i++)
            {
                char *rep;
                rep = (char *) malloc (sizeof(mychars[i][0]));
                strcpy(rep, mychars[i][0]);
                char *with;
                with = (char *) malloc (sizeof(mychars[i][1]));
                strcpy(with, cgichars[i][1]);
                result = (char *) malloc (sizeof(char) * 128);
                result = str_replace(line, rep, with);
            }


            fputs(result, stdout);
        }
    }
    fclose ( file );


    return 0;
}

Valgrind 给了我这个错误:

==4266== Invalid read of size 1
==4266==    at 0x4C286D2: __GI_strlen (mc_replace_strmem.c:284)
==4266==    by 0x5118A8D: fputs (iofputs.c:37)
==4266==    by 0x400A0F: main (repl.c:35)
==4266==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

repl.c 对应于代码末尾以 fputs 开头的行。

此外,mychars 是一个二维数组,如下所示:

char *mychars[NUM][2] = {
  "a", "97",
  "b", "98",
  ....

有人可以告诉我如何解决这个问题吗?此外,任何关于我应该如何改进我当前代码(尤其是使用 malloc)的指针都将不胜感激。

编辑: str_replace 的代码

char *str_replace(char *str, char *orig, char *rep) {
  char buffer[4096];
  char *p;

  if(!(p = strstr(str, orig)))
    return NULL;

  strncpy(buffer, str, p-str);
  buffer[p-str] = '\0';
  sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));

  return buffer;

}

编辑 2 str_replace 和 main 的新代码

出于测试目的,我将 str_replace 方法替换为此处的方法:

在C中替换字符串的功能是什么?

我的主要内容略有变化:

int main() {
    static const char filen[] = "file.txt";
    FILE *file = fopen ( filen, "r" );
    if ( file != NULL )
    {
        char line [ 128 ];
        while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
        {
            int i;
            char *result;
            for(i=0; i< NUM;i++)
            {
                char *rep;
                rep = (char *) malloc (sizeof(mychars[i][0]));
                strcpy(rep, mychars[i][0]);
                char *with;
                with = (char *) malloc (sizeof(mychars[i][1]));
                strcpy(with, mychars[i][1]);
                result = str_replace(line, rep, with);
            }


            fputs(result, stdout);
        }
    }
    fclose ( file );


    return 0;
}

但我仍然得到

==6730== Invalid read of size 1
==6730==    at 0x4C286D2: __GI_strlen (mc_replace_strmem.c:284)
==6730==    by 0x5118A8D: fputs (iofputs.c:37)
==6730==    by 0x400995: main (repl.c:29)
==6730==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

也许最令人沮丧的部分是不知道这些无效读取错误是什么。

编辑 3 我已经更新了 for 循环中心的代码,如下所示:

        int i;
        char* result;
        result = &line[0];
        for(i=0; i< NUM_CGICHARS;i++)
        {
            char *rep;
            rep = (char *) malloc (sizeof(char));
            strcpy(rep, cgichars[i][1]);
            char *with;
            with = (char *) malloc (sizeof(char)*3);
            strcpy(with, cgichars[i][0]);
            result = str_replace(result, rep, with);
            fputs(result, stdout);
            free(rep);
            free(with);
        }

现在我开始得到输出了!然而,仅在两次迭代之后,我得到了一个分段错误,而 valgrind 给了我一大堆这个:

==9130== Invalid read of size 1
==9130==    at 0x4C286D2: __GI_strlen (mc_replace_strmem.c:284)
==9130==    by 0x5118A8D: fputs (iofputs.c:37)
==9130==    by 0x4009DF: main (teststep1.c:27)
==9130==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
4

3 回答 3

7

在这两行

            result = (char *) malloc (sizeof(char) * 128);
            result = str_replace(line, rep, with);

您首先为其分配空间result,然后通过返回str_replace. 该函数可能会返回0,所以你fputs失败了。

顺便说一句,不要强制返回malloc,在 C 中这是多余的,可能会隐藏您忘记包含原型的事实。

编辑:您的str_replace函数在内存处理方面完全错误。永远不要返回指向局部变量的指针,离开函数后该空间无效。

于 2012-04-12T21:27:20.700 回答
0

如果NUM为 0,则result未初始化并且可能是0偶然的。

您没有检查对 的调用结果malloc(),因此失败可能意味着您正在尝试写入NULL指针。

在哪里mychars声明?

于 2012-04-12T21:24:17.183 回答
0

您没有显示如何mychars声明,但是这一行:

rep = (char *) malloc (sizeof(mychars[i][0]))

看起来它可能只分配一个字节。此外,您分配了大量内存并且从不释放它。和这里:

result = (char *) malloc (sizeof(char) * 128);
result = str_replace(line, rep, with);

您使用分配内存malloc,然后通过在其顶部分配另一个函数的返回值来完全丢弃指向该内存的指针。

于 2012-04-12T21:30:59.817 回答