c - Valgrind 报告无效的 Realloc

标签 c valgrind realloc

我正在努力补充我的 C 内存管理知识。我的背景主要是脚本编写和管理,我想了解更多有关 C 和 C++ 的知识。为此,我读了几本书,其中包括使用 realloc 修剪一串空格的示例:

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

char* trim(char* phrase)
{
  char* old = phrase;
  char* new = phrase;

  while(*old == ' ') {
    old++;
  }

  while(*old) {
    *(new++) = *(old++);
  }

  *new = 0;

  return (char*)realloc(phrase, strlen(phrase)+1);
}

int main ()
{
  char* buffer = (char*)malloc(strlen("  cat")+1);
  strcpy(buffer, "  cat");

  printf("%s\n", trim(buffer));

  free(buffer);
  buffer=NULL;

  return 0;
}

我忠实地复制了示例,并使用 c99 -Wall -Wpointer-arith -O3 -pedantic -march=native 编译。我没有遇到任何编译错误,并且该应用程序运行并按照书中所 promise 的进行,但是当我针对 valgrind 运行它时,我收到有关无效 realloc 的错误。

==21601== Memcheck, a memory error detector
==21601== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==21601== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==21601== Command: ./trim
==21601== 
==21601== Invalid free() / delete / delete[] / realloc()
==21601==    at 0x402B3D8: free (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==21601==    by 0x804844E: main (in /home/mo/programming/learning_pointers/trim)
==21601==  Address 0x4202028 is 0 bytes inside a block of size 6 free'd
==21601==    at 0x402C324: realloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==21601==    by 0x80485A9: trim (in /home/mo/programming/learning_pointers/trim)
==21601==    by 0x804842E: main (in /home/mo/programming/learning_pointers/trim)
==21601== 
==21601== 
==21601== HEAP SUMMARY:
==21601==     in use at exit: 4 bytes in 1 blocks
==21601==   total heap usage: 2 allocs, 2 frees, 10 bytes allocated
==21601== 
==21601== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==21601==    at 0x402C324: realloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==21601==    by 0x80485A9: trim (in /home/mo/programming/learning_pointers/trim)
==21601==    by 0x804842E: main (in /home/mo/programming/learning_pointers/trim)
==21601== 
==21601== LEAK SUMMARY:
==21601==    definitely lost: 4 bytes in 1 blocks
==21601==    indirectly lost: 0 bytes in 0 blocks
==21601==      possibly lost: 0 bytes in 0 blocks
==21601==    still reachable: 0 bytes in 0 blocks
==21601==         suppressed: 0 bytes in 0 blocks
==21601== 
==21601== For counts of detected and suppressed errors, rerun with: -v
==21601== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

所以请帮助我理解为什么它被认为是无效的重新分配。这个例子是废话吗?有什么我想念的吗?我知道根据规范,realloc 期望指针先前已由 malloc 创建,这是因为 realloc 在另一个函数中吗?还是因为 valgrind 在不同的函数中而感到困惑?我不是一个彻头彻尾的白痴(大多数时候),但现在我有点像一个没有看到问题的人。

提前致谢!

最佳答案

您正在尝试释放 原始指针,而不是reallocd 指针。您可以通过以下方式修复它:

buffer = trim(buffer)

关于c - Valgrind 报告无效的 Realloc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24458075/

相关文章:

c - 通过代理从网站获取数据

c - 我如何创建一个包含 'Signature Algorithm' 的证书文件使用 openssl api

objective-c - Cocoa Objective-C 主循环

c++ - 使用巨大的 Valgrind 和低级 C++ API

C - 收到 SIGABRT 错误,对我来说没有任何意义

c - 使用 realloc() 会在传递特定内存值后导致段错误

c - 重新分配() : invalid next size glibc detected

C 编程 套接字recv

c++ - 条件跳转或移动取决于 std::wistringstream 的未初始化值

c++ - 使用 g++5 进行的内联构建时出现 valgrind 错误 - valgrind 或 g++5 中的错误?