“Malloc给我返回一个不为空的字符串,但我无法将其清空”

huangapple go评论74阅读模式
英文:

Malloc is giving me a string that isn't empty, and I'm unable to empty it

问题

我正在用C语言编写一个程序,它会遍历一个字符串,将每个字母替换成字母表中它后面第13个字母(类似凯撒密码)。

我在C语言方面相对新手,对于处理字符串感到有些困难。我目前遇到的问题是,当我在第2行初始化一个新字符串时,出现了一些奇怪的东西。我知道这是因为当我在第3行打印字符串时,我看到了3个随机字符(而且它们不总是相同的,在每几次代码执行时都会变化)。

以下是我的代码的相关部分:

    printf("The original string is %s\n", src);
    char *result = malloc(strlen(src) * sizeof(char) + 1);
    printf("Right off the bat, the result string looks like: %s\n", result);
    for (int index = 0; index < strlen(result); index++) 
    {
        result[index] = "
    printf("The original string is %s\n", src);
    char *result = malloc(strlen(src) * sizeof(char) + 1);
    printf("Right off the bat, the result string looks like: %s\n", result);
    for (int index = 0; index < strlen(result); index++) 
    {
        result[index] = "\0";
    }
    printf("Now, the result string looks like: %s\n", result);
";
} printf("Now, the result string looks like: %s\n", result);

我的4-7行的循环尝试通过将字符串中的每个字符都设置为空来清空字符串。更奇怪的是,这个循环不起作用,因为当我在第8行再次打印字符串时,它仍然不是空的。但是现在,它的前4个位置中不再有随机垃圾,而是在分配的空间的前4个位置中重复了4次字母 "j"。

我真的很感激任何关于这个问题的帮助。谢谢。

英文:

I'm writing a program in C that goes through a string and replaces every alphabetical letter with the letter 13 letters after it in the alphabet (so a Caesar cipher, basically).

I'm relatively new to C and I'm having a difficult time working with strings. My issue at the moment is that when I initialize a new string in line 2, for some reason, the string already has random stuff in it. I know this because when I print the string in line 3, I see 3 random characters (and they're not always the same, they change every few code executions).

Here's the relevant part of my code:

    printf(&quot;The original string is %s\n&quot;, src);
    char *result = malloc(strlen(src) * sizeof(char) + 1);
    printf(&quot;Right off the bat, the result string looks like: %s\n&quot;, result);
    for (int index = 0; index &lt; strlen(result); index++) 
    {
        result[index] = &quot;
    printf(&quot;The original string is %s\n&quot;, src);
char *result = malloc(strlen(src) * sizeof(char) + 1);
printf(&quot;Right off the bat, the result string looks like: %s\n&quot;, result);
for (int index = 0; index &lt; strlen(result); index++) 
{
result[index] = &quot;\0&quot;;
}
printf(&quot;Now, the result string looks like: %s\n&quot;, result);
&quot;; } printf(&quot;Now, the result string looks like: %s\n&quot;, result);

My loop in lines 4-7 attempts to empty the string by setting every character in it equal to null. Even more strangely, this loop doesn't work, because when I print the string again in line 8, it's still not empty. Now, though, instead of having random junk in it, it's got the letter "j" repeated 4 times in the first 4 spaces of the allocated space.

I'd really appreciate any help with this. Thanks.

答案1

得分: 0

在这一点上,我们甚至不知道`malloc`是否成功分配了内存。如果成功分配了内存,那么这段内存是未初始化的,使用诸如`strlen`之类的函数会引发[未定义行为](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior)。

如果你希望字符串至少是“空的”,那么第一个字符至少应该是`'
在这一点上,我们甚至不知道`malloc`是否成功分配了内存。如果成功分配了内存,那么这段内存是未初始化的,使用诸如`strlen`之类的函数会引发[未定义行为](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior)。
如果你希望字符串至少是“空的”,那么第一个字符至少应该是`'\0'`。这可以通过使用`calloc`而不是`malloc`来实现。
'`。这可以通过使用`calloc`而不是`malloc`来实现。

char *result = calloc(strlen(src) + 1, 1); // sizeof(char)是1

英文:

>
&gt; char *result = malloc(strlen(src) * sizeof(char) + 1);
&gt;

At this point we don't even know if malloc succeeded in allocating the memory. If it did, the memory is uninitialized and using it with functions like strlen invokes undefined behavior.

If you want the string to be "empty" at the very least the first character needs to be &#39;\0&#39;. This can be achieved by using calloc rather than malloc.

char *result = calloc(strlen(src)+1, 1); // sizeof(char) is 1

huangapple
  • 本文由 发表于 2023年7月3日 11:16:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定