堆腐败,可能的内存泄漏,C + +

use*_*120 0 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;
}
Run Code Online (Sandbox Code Playgroud)

和头文件:

#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);
};
Run Code Online (Sandbox Code Playgroud)

der*_*ert 5

在阅读代码时,我将编辑这几次,请原谅我.看起来你正在实现一个双端队列(dequeue).

构造函数

items    = new item[capacity];
backPtr  = new item;
frontPtr = new item;
midPtr   = new item;
current  = new item;
Run Code Online (Sandbox Code Playgroud)

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

[编辑:似乎项目是一个struct item { int n },你只是复制n周围.你有一个回溯指数和最高指数......]

析构函数

delete frontPtr;
delete backPtr;
delete current;
delete midPtr;
delete [] items; //Heap Corruption Debug Error at the end of program.
Run Code Online (Sandbox Code Playgroud)

自前/后/等.应该指向项目内部,你可以将其中一些项目双重释放.这可能是你的堆腐败崩溃.[编辑:或不,考虑到奇怪的复制]

items        = NULL;
maxSize  =  0;
back         =  0;
Run Code Online (Sandbox Code Playgroud)

这似乎相当愚蠢(对象即将不再;谁在乎?)......

呃,什么

好的,一个简单的dequeue工作的正常方法是有一个元素数组:

items      ->  1   2   3   4   5   6   7   8   9   …
front_ptr  -----------/                   /|\
back_ptr   --------------------------------+
Run Code Online (Sandbox Code Playgroud)

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

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

那将返回3,并将front_ptr提前指向4. pop_back将是相似的(但是测试被反转,并且 - 而不是++).

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