c - 内存泄漏。无法释放 malloc'ed char*

标签 c memory valgrind

这个程序比较存储在动态内存中的两个字符串。

Destroy 函数应该释放所有分配的空间。

当使用 Valgrind 时,它表明我的程序中存在内存泄漏,即存储被比较字符串的 malloc 数据的确切大小。

我不确定我做错了什么。

有人告诉我 pMy_string->data = c_string; 是问题所在,但我不知道为什么或如何解决这个问题。

我试图提交我的其余代码,但堆栈溢出一直说我包含了太多代码并且不允许我提交我的问题,所以我能够包含的只有以下功能。我希望这就足够了。

/* my_string.c */

#include <stdlib.h>
#include <stdio.h>
#include "my_string.h"

/*** STRUCTS ***/
/* define my_string object */
struct my_string {
  int size;
  int capacity;
  char* data;
};
typedef struct my_string My_string;


/*** HELPER FUNCTIONS ***/
/* Counts the length of a given string and     ends at the
    null byte. */
int count_my_string( const char* c_string ){
   int i = 0, count = 0;

  while( c_string[i] != '\0'){
    i += 1;
    count += 1;
  }
  return count;
}
/* Allocate memory to hold a string of    characters. */
/* Returns a pointer to the first byte of     Dynamic 
   Memory. */
MY_STRING my_string_dynamic_mem( int         my_string_size, int my_string_capacity ){
  My_string* pMy_string = NULL;
  pMy_string = ( My_string* )malloc( sizeof( My_string ));
  if ( pMy_string != NULL ){
     pMy_string->size = my_string_size;
     pMy_string->capacity =    my_string_capacity;
     pMy_string->data = ( char* )malloc( pMy_string->capacity * sizeof( char ));
     if ( pMy_string->data == NULL ){
        free( pMy_string );
      pMy_string = NULL;
     }
  }
  else{
    printf( "ERROR: Unable to dynamically allocate memory.\n" );
    free( pMy_string );
    exit( 1 );
  }
  return pMy_string;
}


/* INITIALIZIERS */
/* default init */
MY_STRING my_string_init_default( void ){
   My_string* pMy_string = NULL;
   pMy_string = my_string_dynamic_mem( 0, 7 );
  return pMy_string;
}
/* init for predetermined c-string. */
MY_STRING my_string_init_c_string( char* c_string ){
  My_string* pMy_string = NULL; 
  int my_string_size = 0;
  int my_string_capacity = 0;
  my_string_size = count_my_string( c_string );
  my_string_capacity = ( my_string_size + 1 );
  pMy_string = my_string_dynamic_mem( my_string_size, my_string_capacity );
  pMy_string->data = c_string; 
  return pMy_string;
}

/*** DESTROY ***/
/* Free dynamically allocated memory. */
void my_string_destroy( MY_STRING* phMy_string ){
  My_string* pMy_string = ( My_string* )*phMy_string;
  /* 
  printf("\nThe pMy_string->data pointer is: %p\n",&pMy_string->data );
   printf("The pMy_string pointer is: %p\n",&pMy_string );
  */
  pMy_string->data = NULL;
  free( pMy_string->data );
  free( pMy_string );
  *phMy_string = NULL;
  return;
}

/*** GETTERS ***/
int my_string_get_capacity( MY_STRING hMy_string ){
  My_string* pMy_string = ( My_string* )hMy_string;
  return pMy_string->capacity; 
}

int my_string_get_size( MY_STRING hMy_string ){
  My_string* pMy_string = ( My_string* )hMy_string;
  return pMy_string->size;
}

char* my_string_get_data( MY_STRING hMy_string ){
  My_string* pMy_string = ( My_string* )hMy_string;
  return pMy_string->data;
}

最佳答案

I was told that pMy_string->data = c_string; is the problem but I don't know why or how to fix this.

在 C 中

  • 字符串实际上是 char 数组,包含(至少)'\0' 字符。
  • 数组不能赋值。

所以不要做

pMy_string->data = c_string;

复制数组的元素直到到达'\0':

strcpy(pMy_string->data, c_string); /* The prototype to strcpy() is in string.h. */

关于c - 内存泄漏。无法释放 malloc'ed char*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52463025/

相关文章:

c++ - std::array 的 std::array 是否具有连续内存?

c++ - 如何在valgrind中为进程设置动态链接库路径和环境变量

android - 如何使用 valgrind 启动一个安卓应用程序

c++ - 使用 C++ 字符串时内存泄漏

c - 对函数内的结构数组进行排序

c - 尝试计算 C 中的算术运算时间时出现段错误

c++ - 通过 C++ DLL 将 Arduino 字符串转换为 Excel BSTR 的随机字符

c - 错误: invalid operands to binary ^ (have ‘char *’ and ‘char *’ )

c - 如何解决 malloc() 损坏指针的情况?

c++ - Protobuf对象在传递给动态库时损坏