【问题标题】:Memory leak at allocated/reallocted memory, "5 bytes in 1 blocks are definitely lost"分配/重新分配内存的内存泄漏,“1 个块中的 5 个字节肯定丢失”
【发布时间】:2019-01-09 20:18:06
【问题描述】:

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

我的内存分配不正确吗?

这是 valgrind 的输出:

$ 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] = '\0';

  new_string = (char*) realloc(new_string, strlen(new_string) + 1);

  return new_string;
  free(new_string);
}

int main()
{
  printf("%s\n", cutString("Hello World!", 'o'));
  return 0;
}

我猜我使用 realloc 不正确,但我不知道为什么。
一些帮助将不胜感激,谢谢!

【问题讨论】:

  • free(new_string);return 之后什么都不做。而且您有内存泄漏,因为您没有存储指针,而只是将其提供给printf
  • OT:关于:char *new_string = (char*) malloc(strlen(str) * sizeof(char) + 1); 和类似声明。 1) 在 C 中,返回的类型是void*,可以分配给任何指针。强制转换只会使代码混乱,使其更难以理解、调试等 2) 始终检查 (!=NULL) 返回值以确保操作成功。 3) 关于表达式:`sizeof(char)` 这在 C 标准中被定义为 1。任何东西乘以 1 都没有效果。建议删除那个表达和演员
  • 关于:` return new_string; free(new_string);` 'return' 退出函数,所以对free() 的调用永远不会被执行
  • 关于:new_string = realloc(new_string, strlen(new_string) + 1); 为什么要打扰realloc(),如果分配了稍微过大的堆内存量,这并没有什么坏处
  • 函数中:main(),在调用printf()之后是调用free()的正确位置但是,从函数返回的指针:cutstring()在调用之外不可见到printf()。建议将返回值保存为指针,然后将指针传递给printf()free()

标签: c memory-leaks valgrind


【解决方案1】:

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

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

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

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

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

在 C 中,不可能在不造成内存泄漏的情况下将分配内存的函数流水线化给printf。其他语言有垃圾收集器或对象析构函数,但 C 没有。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-21
    • 2021-03-17
    • 2016-09-12
    • 2014-11-17
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多