나는 거의 완료 한 숙제가 있습니다.힙 손상, 가능한 메모리 누수, C++
내 프로그램이 끝날 때 충돌을 막을 수있는 방법을 알고 싶었던만큼 비효율적이었습니다.
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);
};
나는 * 5 이상의 포맷팅을 읽었으며 실제로는 이해하지 못한다. 누군가가 나에게 마침내 이해할 것이라고 말한다면 아마도. – user40120
모든 코드를 들여 쓰기하여 다시 포맷 할 수 있습니까? –
편집 창에서 "원하는대로"를 올바르게 편집하면 어떻게되지만 결과가 다른지는 다릅니다. – user40120