小编dir*_*tyb的帖子

内存泄漏,因为看似必要的malloc重复

我有以下结构:

typedef struct pair {
  int *key;                // search key for this item
  int *count;                  // pointer to data for this item
  struct pair *next;   // children
} pair_t;

typedef struct counters {
  struct pair *head;
} counters_t;
Run Code Online (Sandbox Code Playgroud)

我有以下功能:

static pair_t * // not visible outside this file
pair_new(const int key)
{

  pair_t *pair = malloc(sizeof(pair_t));

  free(pair->key);
  free(pair->count);

  if (pair == NULL) {
    // error allocating memory for pair; return error
    return NULL;
  } else {

      pair->key = malloc(sizeof(int));
      pair->count …
Run Code Online (Sandbox Code Playgroud)

c pointers memory-leaks dynamic-memory-allocation

1
推荐指数
1
解决办法
98
查看次数