gpt4 book ai didi

c++ - 我怎样才能防止我的 C++ 程序中的内存泄漏?

转载 作者:行者123 更新时间:2023-11-30 05:29:58 25 4
gpt4 key购买 nike

我正在开发一个程序,该程序使用排序队列的双链表实现。此文件是唯一发生内存泄漏的文件。

此外,我不能编辑标题。

我知道要防止内存泄漏,您必须删除使用 new 创建的所有对象。

我的问题是,如果我在 Enqueue(Message msg) 函数的末尾放置一个 delete,如下所示:

void PriorityQ::Enqueue(Message msg)
{
Priorities P = msg.GetPriority();
Node* Location = frontPtr;
Node* PrevLocation = frontPtr;
Node* NewNode = new Node;
NewNode->data = msg;




if (IsFull())
throw FullPQ();
else if (count == 0)
{
NewNode->previousPtr = NULL;
NewNode->nextPtr = NULL;
count++;
frontPtr = NewNode;
rearPtr = NewNode;
}
else
{
Priorities LP = Location->data.GetPriority();
while(LP >= P)
{
if(Location == NULL)
break;

PrevLocation = Location;
Location = Location->nextPtr;

if(Location != NULL)
LP = Location->data.GetPriority();

}//end line 50 while

if(Location == NULL)
{
NewNode->previousPtr = PrevLocation;
NewNode->nextPtr = Location;
PrevLocation->nextPtr = NewNode;
rearPtr = NewNode;
count++;

}
else
{
PrevLocation = Location->previousPtr;
NewNode->previousPtr = PrevLocation;
NewNode->nextPtr = Location;

if(PrevLocation == NULL)
{
Location->previousPtr = NewNode;
count++;
frontPtr = NewNode;
}
else
{
PrevLocation->nextPtr = NewNode;
Location->previousPtr = NewNode;
count++;
}
}// end line 73 else

} //end Line 48 else
delete NewNode;
}//end Enqueue() Function

下次调用 Enqueue(Message msg) 函数时出现段错误,如果我将 delete 放在我的析构函数中:

PriorityQ::~PriorityQ()
{
MakeEmpty();

delete NewNode;

}

它给我这个错误 priorityq.cpp [Error] 'NewNode' was not declared in this scope

所以我的问题是如何在创建对象后不立即释放队列中的对象的情况下防止代码中的内存泄漏。

这是完整的文件。

#include "priorityq.h"


PriorityQ::PriorityQ()
{
frontPtr = NULL;
rearPtr = NULL;
count = 0;

}

PriorityQ::~PriorityQ()
{
MakeEmpty();

delete NewNode;

}

void PriorityQ::MakeEmpty()
{
frontPtr = NULL;
rearPtr = NULL;
count = 0;
}

void PriorityQ::Enqueue(Message msg)
{
Priorities P = msg.GetPriority();
Node* Location = frontPtr;
Node* PrevLocation = frontPtr;
Node* NewNode = new Node;
NewNode->data = msg;




if (IsFull())
throw FullPQ();
else if (count == 0)
{
NewNode->previousPtr = NULL;
NewNode->nextPtr = NULL;
count++;
frontPtr = NewNode;
rearPtr = NewNode;
}
else
{
Priorities LP = Location->data.GetPriority();
while(LP >= P)
{
if(Location == NULL)
break;

PrevLocation = Location;
Location = Location->nextPtr;

if(Location != NULL)
LP = Location->data.GetPriority();

}//end line 50 while

if(Location == NULL)
{
NewNode->previousPtr = PrevLocation;
NewNode->nextPtr = Location;
PrevLocation->nextPtr = NewNode;
rearPtr = NewNode;
count++;

}
else
{
PrevLocation = Location->previousPtr;
NewNode->previousPtr = PrevLocation;
NewNode->nextPtr = Location;

if(PrevLocation == NULL)
{
Location->previousPtr = NewNode;
count++;
frontPtr = NewNode;
}
else
{
PrevLocation->nextPtr = NewNode;
Location->previousPtr = NewNode;
count++;
}
}// end line 73 else

} //end Line 48 else
delete NewNode;
}//end line 27 Enqueue() Function

void PriorityQ::Dequeue()
{
if(IsEmpty())
{
EmptyPQ Empty;
throw Empty;
}

Node* Location = frontPtr;
frontPtr = frontPtr->nextPtr;
Location->nextPtr = NULL;

if(frontPtr != NULL)
{
frontPtr->previousPtr = NULL;
}
else
{
MakeEmpty();
}
if(count != 0)
count--;

}

void PriorityQ::Purge(Priorities p)
{
if(IsEmpty())
{
EmptyPQ Empty;
throw Empty;
}

Node* PurgePtr = frontPtr;
Priorities c = PurgePtr->data.GetPriority();
for(int j = 1; j < count; j++)
{
if(c == p)
{
if(PurgePtr->previousPtr == NULL)
Dequeue();
else if( PurgePtr->nextPtr == NULL)
PurgePtr->previousPtr->nextPtr = NULL;
else
{
PurgePtr->previousPtr->nextPtr = PurgePtr->nextPtr;
PurgePtr->nextPtr->previousPtr = PurgePtr->previousPtr;
}
}// end line 130 if
else
{
PurgePtr = PurgePtr->nextPtr;
c = PurgePtr->data.GetPriority();

}
}// end line 127 for

}

Message PriorityQ::Front() const
{
if(IsEmpty())
throw EmptyPQ();
return frontPtr->data;
}

Message PriorityQ::Rear() const
{
if(IsEmpty())
throw EmptyPQ();
return rearPtr->data;
}

Message PriorityQ::Peek(int n) const
{
if(n <= (count - 1) )
{
Node* PeekPtr = frontPtr;

for(int j = 0; j < n; j++)
{
PeekPtr = PeekPtr->nextPtr;
}

return PeekPtr->data;

}
else
throw InvalidPeekPQ();


}

bool PriorityQ::IsFull() const
{
if(count < 501)
return false;
else
return true;
}

bool PriorityQ::IsEmpty() const
{
if(count == 0 && frontPtr == NULL)
return true;
else
return false;
}

int PriorityQ::Size() const
{

return count;
}

最佳答案

问题是 NewNode 是方法 void Priority::Enqueue 的局部指针。所以它不能从其他方法访问。

当方法完成时,NewNode 被删除,您无法访问它。

关于c++ - 我怎样才能防止我的 C++ 程序中的内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36212853/

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