C valgrind 错误

标签 c string valgrind allocation

下面的代码接受一个输入字符串并将它放在堆上,然后打印它:

   #include <stdio.h>
   #include <stdlib.h>
   #include <string.h>

   int main() {

      char input[50] = "BLA BLBA BLA BLA DJAIO JASJDIOA";
      char *value = (char*) calloc(1, sizeof(char));

      value[0] = '\0';


      for (int i = 0; i < strlen(input); i++) {

          value = (char*) realloc(value, sizeof(char) * (strlen(value) + 2));
          if (value == NULL)
              return 1;

          value[strlen(value)] = input[i];
          value[strlen(value) + 1] = '\0';
      }

      printf("%s\n", value);

      free(value);
      return 0;
 }

工作完美,但出于某种原因,它从 valgrind 中给出了这些错误:

==109423== Memcheck, a memory error detector
==109423== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==109423== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==109423== Command: ./write_test.o
==109423==
==109423== Conditional jump or move depends on uninitialised value(s)
==109423==    at 0x4C2A9E8: strlen (vg_replace_strmem.c:454)
==109423==    by 0x400722: main (in /home/me/testing/write_test.o)
==109423==
==109423== Invalid write of size 1
==109423==    at 0x40072E: main (in /home/me/testing/write_test.o)
==109423==  Address 0x51f4092 is 0 bytes after a block of size 2 alloc'd
==109423==    at 0x4C29B78: realloc (vg_replace_malloc.c:785)
==109423==    by 0x4006E5: main (in /home/me/testing/write_test.o)
==109423==
==109423== Conditional jump or move depends on uninitialised value(s)
==109423==    at 0x4C2A9E8: strlen (vg_replace_strmem.c:454)
==109423==    by 0x4006D2: main (in /home/me/testing/write_test.o)
==109423==
==109423== Conditional jump or move depends on uninitialised value(s)
==109423==    at 0x4C2A9E8: strlen (vg_replace_strmem.c:454)
==109423==    by 0x400703: main (in /home/me/testing/write_test.o)
==109423==
==109423== Conditional jump or move depends on uninitialised value(s)
==109423==    at 0x4C2AA08: __GI_strlen (vg_replace_strmem.c:455)
==109423==    by 0x4E9FEEB: puts (ioputs.c:36)
==109423==    by 0x40075B: main (in /home/me/testing/write_test.o)
==109423==
==109423==
==109423== HEAP SUMMARY:
==109423==     in use at exit: 0 bytes in 0 blocks
==109423==   total heap usage: 32 allocs, 32 frees, 528 bytes allocated
==109423==
==109423== All heap blocks were freed -- no leaks are possible
==109423==
==109423== For counts of detected and suppressed errors, rerun with: -v
==109423== Use --track-origins=yes to see where uninitialised values come from
==109423== ERROR SUMMARY: 123 errors from 5 contexts (suppressed: 0 from 0)

大小为 1 的无效写入是什么?什么条件跳跃或移动取决于未初始化的值?

最佳答案

问题出在这里:

value[strlen(value)] = input[i];  

在上一行之后,value 不再指向以 NUL 结尾的字符串,因此下一行的 strlen 将返回一个不确定的值:

value[strlen(value) + 1] = '\0';

你需要这个:

...
int len = strlen(value);
value[len] = input[i];
value[len + 1] = '\0';
...

关于C valgrind 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43681240/

相关文章:

c - 如何在 C 中创建字符串数组?

在函数中使用 memmove() 连接字符串

ruby - 有没有办法用 %w 创建一个像字符串这样的符号数组?

c - 错误从何而来? Valgrind/C/insertString

c - Realloc 和未初始化的变量 (valgrind)

c - 基本循环意外结束

c - 如何将按钮设置为点击时的默认按钮?

regex - R在字符串内填充0

string - 要求文件为字符串

c - Valgrind 中抑制泄漏意味着什么?