gpt4 book ai didi

c++ - 堆损坏,可能的内存泄漏,C++

转载 作者:太空宇宙 更新时间:2023-11-04 14:59:00 25 4
gpt4 key购买 nike

我有一项作业即将完成。

虽然效率很低,但我只是想知道如何在我的程序结束时防止崩溃。

quack::quack(int capacity) : backPtr( NULL ), frontPtr( NULL )
{
items = new item[capacity];
backPtr = new item;
frontPtr = new item;
midPtr = new item;
current = new item;

maxSize = capacity;
back = maxSize-1;
count = 0;
top = -1;
}

quack::~quack(void)
{
delete frontPtr;
delete backPtr;
delete current;
delete midPtr;
delete [] items; //Heap Corruption Debug Error at the end of program.

items = NULL;
maxSize = 0;
back = 0;
}

bool quack::pushFront(const int n)
{
int i = 0;

if ( count == maxSize ) // Then we cant add to it n e more.
{
throw runtime_error( "Stack is Full" );// Full Stack
return false;
}
backPtr->n = items[back-1].n;
while ( i < count ) // Loop less than however many we counted.
{
if ( i == top+1 )
{
current->n = items[top+1].n;
items[top+1].n = backPtr->n;
}

midPtr->n = items[++i].n;
items[i].n = current->n;

if ( i != back-1 )
{
current->n = items[++i].n;
items[i].n = midPtr->n;
}
}
++count;
items[top+1].n = n;
return true;
}

bool quack::pushBack(const int n)
{
items[count].n = n;
count++;
return true;
}

bool quack::popFront(int& n)
{
n = items[top+1].n;
for ( int i = 0; i < count; i++ )
{
items[i] = items[i+1];
}
count--; // Remove top element.
return true;
}

bool quack::popBack(int& n)
{

n = items[--count].n;
return true;
}

void quack::rotate(int r)
{
int i = 0;

while ( r > 0 ) // rotate postively.
{
frontPtr->n = items[top+1].n;
for ( int i = 0; i < back; i++ )
{
items[i] = items[i+1];
}
items[back-1].n = frontPtr->n;
r--;
}

while ( r < 0 ) // rotate negatively.
{
if ( i == top+1 )
{
backPtr->n = items[back-1].n;
current->n = items[top+1].n;
items[top+1].n = backPtr->n;
}
midPtr->n = items[++i].n;
items[i].n = current->n;

if ( i == back-1 )
{
items[back-1].n = current->n;
i = 0;
r++; continue;
}
else
{
current->n = items[++i].n;
items[i].n = midPtr->n;
if ( i == back-1 )
{
i = 0;
r++; continue;
}
}
}
}

void quack::reverse(void)
{
int j = 0; // Variable declaration/initialization.

frontPtr->n = items[top+1].n;
backPtr->n = items[back-1].n;

for ( int i = 0; i < count / 2; i++ )
{
items[j].n = items[i].n;
items[i].n = items[ count - i-1 ].n;
items[ count - i-1 ].n = items->n;
}

items[top+1].n = backPtr->n;
items[back-1].n = frontPtr->n;
}

int quack::itemCount(void)
{
return count;
}

ostream& operator<<(ostream& out, quack& q)
{
if ( q.count == 0 ) // No elements have been counted.
out << endl << "quack: empty" << endl;
else
{
out << endl << "quack: ";
for ( int i = 0; i < q.count; i++ )
{
if ( i < q.count-1 )
out << q.items[i].n << ", ";
else out << q.items[i].n;
}
out << endl << endl;
}
return out;
}

和头文件:

#include <ostream>

using namespace std;

class quack
{
public:
quack(int capacity);
~quack(void);
bool pushFront(const int n); // Push an item onto the front.
bool pushBack(const int n); // Push an item onto the back.
bool popFront(int& n); // Pop an item off the front.
bool popBack(int& n); // Pop an item off the back.
void rotate(int r); // "rotate" the stored items (see note below).
void reverse(void); // Reverse the order of the stored items.
int itemCount(void); // Return the current number of stored items.

private:
int maxSize; // is for the size of the item stack
int back; // is for the back or "bottom" of the stack
int count; // to count the items added to the stack
int top;

struct item // Definition of each item stored by the quack.
{
int n;
};

item *items; // Pointer to storage for the circular array.
item *backPtr;
item *frontPtr;
item *midPtr;
item *current;

public:
friend ostream& operator<<(ostream& out, quack& q);
};

最佳答案

我将在通读代码时对其进行几次编辑,请原谅。看来您正在实现双端队列(出队)。

构造函数

items    = new item[capacity];
backPtr = new item;
frontPtr = new item;
midPtr = new item;
current = new item;

这没有意义。您的前/后/中/当前指针实际上并不指向您的项目之一。您可能想要 frontPtr = items+0backPtr = items + capacity-1(反之亦然)。不确定出队需要 midPtr 或 current 的目的。

[编辑:似乎 item 是一个 struct item { int n } 而你只是在复制 n 。你有一个后索引和顶部索引...]

析构函数

delete frontPtr;
delete backPtr;
delete current;
delete midPtr;
delete [] items; //Heap Corruption Debug Error at the end of program.

自前面/后面/等等。应该指向项目内部,您正在双重释放其中一些项目。那可能是您的堆损坏崩溃。 [编辑:与否,考虑奇怪的复制]

items        = NULL;
maxSize = 0;
back = 0;

这看起来很愚蠢(对象将不再存在;谁在乎呢?)...

呃,什么

好的,一个简单的出列的正常工作方式是有一个元素数组:

items      ->  1   2   3   4   5   6   7   8   9   …
front_ptr -----------/ /|\
back_ptr --------------------------------+

然后您将有一个指针 (frontPtr) 指向数组中第一个使用的点,另一个指针 (backPtr) 指向最后一个使用的点。所以,一个 pop_front 会做这样的事情:

if (frontPtr <= backPtr) {
return *frontPtr++
} else {
// tried to pop from empty dequeue, handle error
}

这将返回 3,并将 front_ptr 提前指向 4。pop_back 将是类似的(但测试相反,并且使用 -- 而不是++)。

或者,您可以存储索引而不是指针。但是选择一个,不要同时使用索引和指针。

关于c++ - 堆损坏,可能的内存泄漏,C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1449329/

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