c - 我的堆栈实施中的数据泄漏

时间:2016-03-15 12:59:46

标签: c memory-leaks

我的代码中找不到任何泄漏。我也相信我错误地实现了一个堆栈。以下是valgrind的相关文件和错误。此外,任何编码约定或提示都将受到赞赏,因为我仍然从动态分配和指针开始。

stack.c

#include <stdlib.h>
#include "stackNode.h"
#include "stack.h"

void push(StackNode** stack, char* data) {
  StackNode* n = makeStackNode(data, *stack);
  stack = &n;
}

char* top(StackNode* stack) {
  if (emptyStack(stack) == 0) {
    return stack->data;
  }
  exit(0);
}

void pop(StackNode** stack) {
  if (emptyStack(*stack) == 0) {
    StackNode* temp = *stack;
    stack = &((*stack)->next);
    free(temp->data);
    free(temp->next);
    free(temp);
  }
  exit(0);
} 

int emptyStack(StackNode* stack) {
  if (stack == NULL) {
    return 1;
  }
  return 0;
}

stackNode.h

#ifndef STACK_NODE_H
#define STACK_NODE_H

typedef struct StackNode {
    char* data;     // data associated with the node    
    struct StackNode* next; // pointer to next node (NULL if none)
} StackNode;

StackNode* makeStackNode(char* data, StackNode* next);

#endif

stackNode.c

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

#include "stackNode.h"

StackNode* makeStackNode(char* data, StackNode* next) {
  StackNode* res = NULL;
  res = malloc(sizeof(StackNode));

  res->data = malloc(strlen(data) + 1);
  strcpy(res->data, data);

  res->next = malloc(sizeof(StackNode));
  res->next = next;

  return res;
} 

interp.c(主要功能)

#include "interp.h"
#include "stackNode.h"
#include "stack.h"

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

int main(int argc, char* argv[]) {
  StackNode* stack = NULL;
  push(&stack, "fire");
  push(&stack, "test");
  while(emptyStack(stack) == 0) {
    char* tk = top(stack);
    pop(&stack);
    printf("%s", tk);
  }
  return EXIT_SUCCESS;
}

Valgrind错误

==7811== Memcheck, a memory error detector
==7811== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==7811== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==7811== Command: ./interp
==7811==
==7811==
==7811== HEAP SUMMARY:
==7811==     in use at exit: 42 bytes in 4 blocks
==7811==   total heap usage: 4 allocs, 0 frees, 42 bytes allocated
==7811==
==7811== 21 (16 direct, 5 indirect) bytes in 1 blocks are definitely lost in    loss record 3 of 4
==7811==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7811==    by 0x400C40: makeStackNode (stackNode.c:9)
==7811==    by 0x400B54: push (stack.c:7)
==7811==    by 0x4007C8: main (interp.c:11)
==7811==
==7811== 21 (16 direct, 5 indirect) bytes in 1 blocks are definitely lost in loss record 4 of 4
==7811==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7811==    by 0x400C40: makeStackNode (stackNode.c:9)
==7811==    by 0x400B54: push (stack.c:7)
==7811==    by 0x4007D9: main (interp.c:12)
==7811==
==7811== LEAK SUMMARY:
==7811==    definitely lost: 32 bytes in 2 blocks
==7811==    indirectly lost: 10 bytes in 2 blocks
==7811==      possibly lost: 0 bytes in 0 blocks
==7811==    still reachable: 0 bytes in 0 blocks
==7811==         suppressed: 0 bytes in 0 blocks
==7811==
==7811== For counts of detected and suppressed errors, rerun with: -v
==7811== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

1 个答案:

答案 0 :(得分:1)

res->next = malloc(sizeof(StackNode));

可能应该只是

res->next = NULL;

如果之后被分配,则完全删除。

同样pop不应free next项,否则您将获得双倍免费。