为什么我会在此C代码中获得具有动态分配的char数组的其他怪异字符?

发布于 2025-02-08 07:57:59 字数 822 浏览 2 评论 0原文

我通常是动态内存分配的新手。 在过去的三天中,我一直在寻找此代码中的错误,这使我发疯了,这就是为什么我决定在这里寻求帮助的原因。 这是代码:

char ch;
char* line=(char*)calloc(1, sizeof(char));

if(input!=NULL) {
    for(int num=1; (ch=fgetc(input)) != EOF; num++) //input is the pointer to the in file
        if(ch!=' ') {
            line=(char*)realloc(line, sizeof(char)*num+1);
            strcat(line, &ch);
        }
        else
            break;
}

我正在尝试从文件中读取两个分隔式的单词中的第一个,其中总尺寸未预定(我需要从文件中读取更多的内容,因此重要的是,这很重要,这只是“只是尝试”)。 这是针对单行的,而不是多行(我认为在这种情况下会使用char ** **),这个想法是分配行的第一个字符并将其设置为零,然后通过将内存重新分配为一个角色。 如果我“ num ++”,它会崩溃;如果我不这样相同的?)。我检查了ASCII代码,这就是我得到的:78 1 111 1 108 1 101 1;每个字符之后都有一个“ 1”,其值与“ num”相同(实际上,如果num == 2,那么我得到'2而不是'1)。我已经使用不同的编译器和不同的机器尝试了它,但是我总是得到相同的结果,我无法解释原因。 我真的很疯狂,也是因为我将在大约两个星期内进行考试,这基本上是我在所有必需的主题中尚未学到的唯一一件事。

非常感谢您提前

I'm quite new to dynamic memory allocation in general.
I've been looking for an error in this code for about 6 hours in the last 3 days now, it's driving me crazy, that's why I've decided to ask for help here.
Here's the code:

char ch;
char* line=(char*)calloc(1, sizeof(char));

if(input!=NULL) {
    for(int num=1; (ch=fgetc(input)) != EOF; num++) //input is the pointer to the in file
        if(ch!=' ') {
            line=(char*)realloc(line, sizeof(char)*num+1);
            strcat(line, &ch);
        }
        else
            break;
}

I'm trying to read from a file the first of two whitespace-separated words, where the total size is not predetermined (I'll need this to read even more from the file so it's important, this was "just to try").
This is for a single line, not multiple lines (char** I think would be used in that case), and the idea was to allocate the first character of the line and set it to zero, then reallocate the memory incrementing its size by one character.
If I "num++", it crashes; if I don't, its output will be, instead of "Nole", this: N☺o☺l☺e☺ (output is after the loop; how does it even increase if num remains the same?). I checked the ASCII codes and this is what I get: 78 1 111 1 108 1 101 1; there is a '1' after every character, which is THE SAME value as "num" (in fact, if num==2, then I get '2's instead of '1's). I've tried it with different compilers and different machines but I always get the same result and I cannot explain why.
I'm really going crazy, also because I'm gonna have an exam in about two weeks and this is basically the only thing I haven't learned yet among all the required topics.

Thank you so much in advance ????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热情消退 2025-02-15 07:58:00
  • eofint,因此您必须使用int ch;

  • 如注释中所述,您将单个ch传递给strcat而不是终止字符串,因此它会发出干扰。快速修复:strcat(line,(char [2]){ch,'\ 0'});

    或者如果添加计数器,则可以做行[Count] = CH;,这要高得多。尽管在这种情况下,您必须记住在最后手动附加零终端。

另外,sizeof(char)始终按sizeof的定义为1,因此这只是一种不必要的blo缩编写1

  • EOF is an int so you must use int ch;

  • As mentioned in comments, you pass a single ch to strcat and not a null terminated string, so it will go haywire. Quick fix: strcat(line, (char[2]){ch,'\0'});.

    Or if you add a counter, you could just do line[count] = ch; which is much more efficient. Though in that case you'll have to remember to append the null terminator manually in the end.

Also, sizeof(char) is always 1 by the very definition of sizeof, so it's just a needlessly bloated way of writing 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文