c语言 - 分配/重新分配内存的内存泄漏,"5 bytes in 1 blocks are definitely lost"



我在检查程序内存泄漏时遇到valgrind错误。分配/重新分配内存时,错误发生在我的cutString函数中的某个地方,但我不确定我做错了什么。

我是否分配了不正确的内存?

这是瓦尔格林德输出:

$ valgrind --leak-check=full --track-origins=yes ./cutstring
==7017== Memcheck, a memory error detector
==7017== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.           
==7017== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info                                     
==7017== Command: ./cutstring
==7017==
Hell
==7017==
==7017== HEAP SUMMARY:
==7017==     in use at exit: 5 bytes in 1 blocks
==7017==   total heap usage: 3 allocs, 2 frees, 1,042 bytes allocated                                           
==7017==
==7017== 5 bytes in 1 blocks are definitely lost in loss record 1 of 1                          
==7017==    at 0x4839D7B: realloc (vg_replace_malloc.c:826)
==7017==    by 0x109205: cutString (in cutstring)
==7017==    by 0x109228: main (in cutstring)
==7017==
==7017== LEAK SUMMARY:
==7017==    definitely lost: 5 bytes in 1 blocks
==7017==    indirectly lost: 0 bytes in 0 blocks
==7017==      possibly lost: 0 bytes in 0 blocks
==7017==    still reachable: 0 bytes in 0 blocks
==7017==         suppressed: 0 bytes in 0 blocks
==7017==
==7017== For counts of detected and suppressed errors, rerun with: -v
==7017== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

这是我的代码:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char *cutString(char *str, char del)
{ 
  char *new_string = (char*) malloc(strlen(str) * sizeof(char) + 1);
  int i = 0;
  while (str[i] != del)
  {
    new_string[i] = str[i];
    i++;
  }
  new_string[i] = '';
  new_string = (char*) realloc(new_string, strlen(new_string) + 1);
  return new_string;
  free(new_string);
}
int main()
{
  printf("%sn", cutString("Hello World!", 'o'));
  return 0;
}

猜我错误地使用了 realloc,但我无法弄清楚为什么。
一些帮助将不胜感激,谢谢!

cutString必须分配内存并返回它。当然(幸运的是),无条件return之后的所有语句都没有达到。

  return new_string;
  free(new_string);   // never executed
}

幸运的是!因为否则你会返回未分配的内存:未定义的行为。

这里的问题是您将返回的值传递给 printf ,但在该调用之后,指针丢失。您必须存储它才能释放它,但只有在打印后。

int main()
{
  char *s = cutString("Hello World!", 'o'));
  printf("%sn", s);
  free(s);
  return 0;
}

在 C 语言中,不可能在不创建内存泄漏的情况下将内存分配给printf的函数。其他语言有垃圾回收器或对象析构函数,但 C 语言没有。

最新更新