c - 链接堆栈没有链接?

时间:2016-03-16 03:18:31

标签: c memory-management stack

我的课程在c中被分配了一个堆栈实现。出于一些令人遗憾的原因,当我推入堆栈然后一次一个地弹出它们时,它只会在停止之前弹出一个。 Valgrind告诉我这是由于记忆问题,但我似乎无法确定它。

stack.c

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

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

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

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

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

stackNode.c

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

#include "stackNode.h"

StackNode* makeStackNode(char* data, StackNode* next) {
  StackNode* res = malloc(sizeof(StackNode));
  res->data = malloc(strlen(data) + 1);
  strcpy(res->data, data);
  res->next = next;
  return res;
} 

的main.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, "A");
  push(&stack, "B");
  push(&stack, "C");
  while(emptyStack(stack) == 0) {
    char* tk = top(stack);
    printf("%s\n", tk);
    pop(&stack); 
  } 
  return EXIT_SUCCESS;
}

的valgrind

==17494== HEAP SUMMARY:
==17494==     in use at exit: 36 bytes in 4 blocks
==17494==   total heap usage: 6 allocs, 2 frees, 54 bytes allocated
==17494==
==17494== 2 bytes in 1 blocks are still reachable in loss record 1 of 4
==17494==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17494==    by 0x400C4D: makeStackNode (stackNode.c:9)
==17494==    by 0x400B57: push (stack.c:6)
==17494==    by 0x4007C4: main (interp.c:10)
==17494==
==17494== 2 bytes in 1 blocks are still reachable in loss record 2 of 4
==17494==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17494==    by 0x400C4D: makeStackNode (stackNode.c:9)
==17494==    by 0x400B57: push (stack.c:6)
==17494==    by 0x4007D5: main (interp.c:11)
==17494==
==17494== 16 bytes in 1 blocks are still reachable in loss record 3 of 4
==17494==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17494==    by 0x400C31: makeStackNode (stackNode.c:8)
==17494==    by 0x400B57: push (stack.c:6)
==17494==    by 0x4007C4: main (interp.c:10)
==17494==
==17494== 16 bytes in 1 blocks are still reachable in loss record 4 of 4
==17494==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17494==    by 0x400C31: makeStackNode (stackNode.c:8)
==17494==    by 0x400B57: push (stack.c:6)
==17494==    by 0x4007D5: main (interp.c:11)
==17494==
==17494== LEAK SUMMARY:
==17494==    definitely lost: 0 bytes in 0 blocks
==17494==    indirectly lost: 0 bytes in 0 blocks
==17494==      possibly lost: 0 bytes in 0 blocks
==17494==    still reachable: 36 bytes in 4 blocks
==17494==         suppressed: 0 bytes in 0 blocks

1 个答案:

答案 0 :(得分:2)

您忘记从函数pop()返回,即使节点有效,程序也会退出。

void pop(StackNode** stack) {
  if(emptyStack(*stack) == 0) {
    StackNode* tmp = *stack;
    *stack = (*stack)->next;
    free(tmp->data);
    free(tmp);
    return; /* add this */
  }
  exit(EXIT_FAILURE);
}