Search code examples
c++templatesmemory-leaksdynamic-memory-allocation

Memory loss with multidimensional dynamic array


I have class "List" that is just dynamic array made with help of templates. In my task I need to have List of List of strings, everything is going fine and work, but memory is leaking in it. I also have List of entities in my code, but its memory is freed if I understand correctly what valgrind says. I do think that the problem is in multidimensionality. I even may be undesrtand exctly why it happens, but I don't know how to fix it.

list

#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

example of using

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);
    }
}

Solution

  • You are leaking all the allocations from

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

    because you never call delete on l. Don't use new here at all. Just declare the List<string> as automatic variable:

    List<string> l;
    

    Then your class also violates the rule of 0/3/5 because it doesn't define a copy assignment operator.


    The test if (_array == nullptr) after _array = new T[I_CAP]; is pointless. If the allocation fails then new will throw an exception and if it doesn't fail it will never return a nullptr.

    The same holds for T *temp = new T[newSize]; if (temp == NULL) in resize.


    There may be more issues.