gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:01:19 25 4
gpt4 key购买 nike

显然我在代码中找不到一些泄漏。我还相信我错误地实现了堆栈。以下是 valgrind 的相关文件和错误。此外,任何编码约定或技巧将不胜感激,因为我仍在从动态分配和指针开始。

堆栈.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)

最佳答案

这个

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

也许应该是

res->next = NULL;

或者如果之后分配则完全删除。

同样pop不应该是free荷兰国际集团next项目,否则您将获得双倍免费。

关于c - 我的堆栈实现中的数据泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36012054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com