gpt4 book ai didi

c++ - 当我调用解构函数时,这会导致内存泄漏吗?

转载 作者:搜寻专家 更新时间:2023-10-31 01:37:51 25 4
gpt4 key购买 nike

我有一个数据结构项目,我必须为我的 Uni 类做这个项目,它是用链表实现一个堆栈;简单的东西。我们在代码方面得到了一些帮助,向我们展示了实现这种结构的正确方法。

堆栈类:

class Stack
{
public:
Stack(void)
{
top = NULL; // Initialises defualt top node pointer.
}

~Stack(void)
{
while (NodePop() != NULL){}
}

void Push(int value) // Pushes a new node onto the stack.
{
Node* temp = new Node(value, top); // Creates a temporary node with a value and makes it
// point to the top node as the next node in the stack.

top = temp; // Temporary node becomes the top node in the stack.
}

Node* NodePop(void)
{
/* Remove top node from the stack */
Node* temp = top; // Creates a temporary node to return, sets it to top node.
if (top != NULL) top = top->getNext(); // Points the stacks top node to the next node in the list.
return temp; // Returns a pointer to the popped node still in the heap.
}

int Pop(void) // Pops the top node off of the stack. Returns the nodes value.
{
Node* temp = NodePop();
int valueReturn = 0;

/* Sets the return value */
if (temp != NULL)
{
valueReturn = temp->getVal(); // Set return value to the nodes value if there is a node left.
}
else
{
throw "Stack Empty"; // Throws exception if temp is NULL and stack is empty.
}

delete temp; // Deletes the node entirely from the heap.

return valueReturn;
}

private:
Node* top;

};

节点类:

class Node
{
public:
Node(int value, Node* nextptr = NULL, Node* prevptr = NULL, int currentpriority = 0)
{
/* Set initial variables for the node at creation */
this->value = value;
this->next = nextptr;
this->prev = prevptr;
this->priority = currentpriority;
}

// bunch of getters and setters...

private:
Node* next; // Pointer to the next node.
Node* prev; // Pointer to the previous node.
int priority; // Stores the node priority as a number 0-9.
int value; // Stores the node value for printing.

};

我们不能更改任何类结构(我也很烦恼,NodePop() 应该是私有(private)的,但是 w/e)。

所以这里的 NodePop() 本质上是从列表中移除顶部节点,但并不删除它;它从链表中删除所有对它的引用,但它从不从堆中删除它,它只是在 Pop() 中从堆中删除。一切都很好(除了能够公开调用 NodePop() 之外,但同样,我不允许将其设为私有(private))。但是当我调用析构函数时必须使用 NodePop(),而不是 Pop()。

那么这是否意味着当 NodePop() 从析构函数运行时,节点永远不会从堆中删除?

如果是这样,我将如何删除它们,因为它会运行 nodePop(),如果我有一段时间、do-while 或 if 语句条件,那么总会有一个节点未删除?

最佳答案

查看有问题的代码

~Stack(void)
{
while (NodePop() != NULL){}
}

Node* NodePop(void)
{
/* Remove top node from the stack */
Node* temp = top; // Creates a temporary node to return, sets it to top node.
if (top != NULL) top = top->getNext(); // Points the stacks top node to the next node in the list.
return temp; // Returns a pointer to the popped node still in the heap.
}

您的析构函数调用 NodePop() 直到该函数返回 NULL。让我们看看 NodePop() 做了什么。代码中的注释声称它 Creates a temporary node to return 这不是真的。它创建一个指向节点(节点*)的指针,并将该指针设置为指向 top 所指向的同一位置。如果 top 不为 null,它会将 top 设置为指向 top 的下一个节点。它返回 temp是指向最初是顶级节点的指针

在任何时候您都不会释放与任何节点关联的内存,所以是的,存在内存泄漏。

您可以通过删除在非 NULL 的析构函数中遇到的每个 Node* 来修复泄漏。

关于c++ - 当我调用解构函数时,这会导致内存泄漏吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33641661/

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