【问题标题】:What have I missed, what cause the memory leak C++我错过了什么,是什么导致内存泄漏 C++
【发布时间】:2018-06-17 18:02:19
【问题描述】:

我无法理解我的内存泄漏在我的项目中的位置。 我构建的模板如下所示:

#pragma once
#include "IHeap.h"
#include <iostream>

using namespace std;

template <typename T>
class dHeap
{
public:
    dHeap(T size);
    dHeap(T size, int nr);
    dHeap(const dHeap &original);
    ~dHeap();
    dHeap<T>& operator=(const dHeap<T> &original);

    void deepCopy(const dHeap &original);
    void push(const T &item);
    T pop();
    T peek()const;
    int size()const;
    int getdValue()const;
    void printAll()const;

    void heapify(int arr[], int size, int root);
    void heapSort(int arr[], int size);

private:
    //T nr;
    T *arrHeap;
    T nrOfItems;
    T capacity;
    T dValue;

    void expandHeap();
};

template<typename T>
inline dHeap<T>::dHeap(T size)
{
    capacity = size;
    arrHeap = new T[capacity + 1];
    nrOfItems = 0;
    dValue = size;

}

template<typename T>
inline dHeap<T>::dHeap(T size, int nr)
{
    capacity = size;
    arrHeap = new T[nr];
    nrOfItems = 0;
    dValue = size;
}

template<typename T>
inline dHeap<T>::dHeap(const dHeap &original)
{
    this->deepCopy(original);
}

template<typename T>
inline dHeap<T>::~dHeap()
{
    delete[] arrHeap;
}

template<typename T>
inline dHeap<T>& dHeap<T>::operator=(const dHeap<T>& original)
{
    if (this != &original)
    {
        this->deepCopy(original);
    }
    return *this;
}

template<typename T>
inline void dHeap<T>::expandHeap()
{
    capacity *= 2;
    T *temp = new T[capacity];

    for (int i = 0; i < nrOfItems; i++)
    {
        temp[i] = arrHeap[i];
    }   

    delete[] arrHeap;
    arrHeap = temp;
}

template<typename T>
inline void dHeap<T>::deepCopy(const dHeap &original)
{
    capacity = original.capacity;
    nrOfItems = original.nrOfItems;
    arrHeap = new T[capacity];
    dValue = original.dValue;

    for (int i = 0; i < original.nrOfItems; i++)
    {
        this->arrHeap[i] = original.arrHeap[i];
    }
}

template<typename T>
inline void dHeap<T>::push(const T &item)
{
    if (nrOfItems >= capacity)
    {
        expandHeap();
    }

    arrHeap[nrOfItems] = item;
    nrOfItems++;
}

template<typename T>
inline T dHeap<T>::pop()
{
    int removed = arrHeap[0];
    arrHeap[0] = arrHeap[nrOfItems - 1];

    nrOfItems--;

    return removed;
}

template<typename T>
inline T dHeap<T>::peek() const
{
    return arrHeap[0];
}

template<typename T>
inline int dHeap<T>::size() const
{
    return this->nrOfItems;
}

template<typename T>
inline int dHeap<T>::getdValue() const
{
    return this->dValue;
}

template<typename T>
inline void dHeap<T>::printAll() const
{
    for (int i = 0; i < nrOfItems; i++)
    {
        cout << "Heap element " << i << ". " << arrHeap[i] << endl;
    }
}

template<typename T>
inline void dHeap<T>::heapSort(int arr[], int size)
{
    for (int j = 0; j < size; j++)
    {
        // Build heap - which means rearrange array
        for (int i = size / 2 - 1; i >= 0; i--)
        {
            heapify(arrHeap, size, i);
        }

        for (int i = size - 1; i >= 0; i--)
        {
            swap(arrHeap[0], arrHeap[i]);
            heapify(arrHeap, i, 0);
        }

        //when re-structured heap, use pop and re-do it again until done
        arr[j] = pop();
    }
}
template<typename T>
inline void dHeap<T>::heapify(int arr[], int n, int root)
{
    int largest = root;  
    int leftChild = 2 * root + 1;
    int rightChild = 2 * root + 2;

    // If left child is larger than root
    if (leftChild < n && arr[leftChild] > arr[largest])
    {
        largest = leftChild;
    }

    // If right child is larger than largest so far
    if (rightChild < n && arr[rightChild] > arr[largest])
    {
        largest = rightChild;
    }

    // If largest is not root, heapify recursivly until done
    if (largest != root)
    {
        swap(arr[root], arr[largest]);
        heapify(arr, n, largest);
    }
}

我有一个叫做 heapArr 的指针,我用它来建立一个堆。当程序终止时,将调用析构函数,并且我放置了一个 delete[] this->heapArr 声明以在程序完成时删除指针。 而且我还在扩展函数中添加了一个delete[] this->heapArr,以便在分配新的扩展数组之前释放内存。

我不确定我是否完美地解释了这一点,但问题是我似乎错过了删除某些内容,因为我在结束程序时收到了内存泄漏警告。 我错过了什么?

【问题讨论】:

  • 你在深拷贝操作中遗漏了一件事。
  • 调用operator=可能有内存泄漏,因为在调用deepCopy函数之前没有删除内存。
  • 谢谢你们,错过了那部分。我只是创建了一个 freememory 函数并在运行 deepcopy 代码部分之前调用它并且内存泄漏消失了
  • dHeap(T size); 这没有多大意义。你能用std::strings 测量尺寸吗?

标签: c++ class templates memory-management memory-leaks


【解决方案1】:

deepCopy 中的内存泄漏,您在其中分配新内存而不取消分配旧内存。

话虽如此,不要自己分配内存。您的大部分代码都在复制std::vector 的功能,因此请使用std::vector&lt;T&gt; 而不是T*(如果由于某种原因你不能使用std::vector,我建议你实现一个替换。通过将内存管理与堆逻辑分开来分而治之。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多