多维动态数组的内存丢失

问题描述 投票:0回答:1

我有“ List”类,它只是借助模板制作的动态数组。在我的任务中,我需要字符串列表列表,一切正常,并且可以工作,但是内存泄漏了。我的代码中也有实体列表,但是如果我正确理解valgrind所说的话,它将释放其内存。我确实认为问题出在多维上。我什至可能都不希望知道为什么会发生这种情况,但是我不知道如何解决它。

列表

#ifndef ____LIST__HJKIPU13

#define ____LIST__HJKIPU13

#define I_CAP 8

#include <iostream>

using namespace std;

template <typename T>

class List {
    T *_array;
    int _capacity;
    int _size;

public:

    List ()
    {
        _array = new T[I_CAP];
        if (_array == nullptr) {
            cerr << "Error, in constructor (dynamic_array.cpp, line 8)";
            exit(1);
        }
        _capacity = I_CAP;
        _size = 0;
    }

    List (const List &other)
    {
        _array = new T[other._capacity];
        if (_array == nullptr) {
            cerr << "Error, in copy constructor (dynamic_array.cpp, line 8)";
            exit(1);
        }
        _capacity = other._capacity;
        _size = other._size;
        copy(other._array, other._array+other._size, _array);
    }

    ~List () {delete[] _array;}

    //--------------------------------------------------------------------------------

    int size () {return _size;}

    void resize (int newSize)
    {
        T *temp = new T[newSize];
        if (temp == NULL) {
            cerr << "Error, in function resize (dynamic_array.cpp, line 27)";
            exit(1);
        }
        std::copy(_array, _array+_capacity, temp);
        delete[] _array;
        _array = temp;
        _capacity = newSize;
    }

    //--------------------------------------------------------------------------------

    T &at (int index)
    {
        if (_size <= index) {
            cerr << "Error, in function get (BList.cpp, line 22)";
            exit(1);
        }
        return _array[index];
    }

    T &operator[] (int index) {return at(index);}

    void insert (int index, T &data)
    {
        if (index > _size) {
            cerr << "Error, in function insert (list.cpp, line 40)";
            exit(1);
        }
        if (isFull()) {
            resize(_capacity*2);
        }

        shitright(index);
        _array[index] = data;
        _size++;
    }

    void removeAt (int index)
    {
        if (index >= _size) {
            cerr << "Error, in function removeAt (list.cpp, line 55)";
            exit(1);
        }
        shitleft(index);
        _size--;
    }

    //--------------------------------------------------------------------------------

    void add (T &data)
    {
        if (isFull()) {
            resize(_capacity*2);
        }

        _array[_size] = data;
        _size++;
    }

    void remove (T &data)
    {
        int index = indexOf(data);
        if (index == -1) {
            std::cerr << "Error " << endl;
            exit(1);
        }

        removeAt(index);
    }

    int  indexOf (T &data)
    {
        int index = -1;

        for (int i = 0; i < _size; i++) {
            if (_array[i] == data) {index = i; break;}
        }

        return index;
    }

    bool contains (T &data)
    {
        if (indexOf(data) == -1) return false;
        else return true;
    }

    //--------------------------------------------------------------------------------

    bool isEmpty () {return static_cast<bool>(_size);}

    void clear () {_size = 0;}

    bool isFull () {return _size == _capacity;}

    //--------------------------------------------------------------------------------

    T shitleft(int index)
    {
        if (index >= _capacity || _size >= _capacity || _size <= index) {
            cerr << "Error, in function shiftleft (dynamic_array.cpp, line 51)";
            exit(1);
        }
        T temp = _array[index];
        for (int i = index; i < _size-1; i++) {
            _array[i] = _array[i+1];
        }

        return temp;
    }

    void shitright(int index)
    {
        if (index >= _capacity || _size >= _capacity || _size <= index) {
            cerr << "Error, in function shiftright (dynamic_array.cpp, line 65)";
            exit(1);
        }

        if (isFull) resize(_capacity*2);

        for (int i = _size; i > index; i--) {
            _array[i] = _array[i-1];
        }
    }

    //

    friend ostream &operator<< (ostream &stream, List<T> &list)
    {
        for (int i = 0; i < list._size; i++) {
            stream << i << ":  <={ " << list._array[i] << " }=>  ";
        }
        if (list._size == 0) stream << ": {empty}" << endl;
        return stream;
    }
};

#endif

使用示例

void entitytoTable (List<List<string>> &list, List<Book> &bookList)
{
    for (int i = 0; i < bookList.size(); i++) {
        string temp;
        List<string> *l = new List<string>;
        temp = to_string(bookList[i].id);
        (*l).add(temp);
        temp = bookList[i].title;
        (*l).add(temp);
        temp = to_string(bookList[i].pages);
        (*l).add(temp);
        temp = to_string(bookList[i].price);
        (*l).add(temp);
        temp = to_string(bookList[i].nChar);
        (*l).add(temp);
        list.add(*l);
    }
}
c++ templates memory-leaks dynamic-memory-allocation
1个回答
1
投票

您正在泄漏所有分配自

List<string> *l = new List<string>;

因为您从未在delete上呼叫l。完全不要在此处使用new。只需将List<string>声明为自动变量即可:

List<string> l;

然后您的类也违反了rule of 0/3/5,因为它未定义副本分配运算符。


if (_array == nullptr)之后的测试_array = new T[I_CAP];是没有意义的。如果分配失败,则new将引发异常;如果分配失败,则将永远不会返回nullptr

T *temp = new T[newSize]; if (temp == NULL)中的resize同样成立。


可能还有更多问题。

© www.soinside.com 2019 - 2024. All rights reserved.