1

I am writing a DynamicArray class and got stuck with resize() function. It works as expected when I remove line delete[] temp. But when I don't it seems copying only the last 2 elements. Am I missing something here??

#include <iostream>
#include <initializer_list>



template <typename T, int N>
class DynamicArray
{
private:
    T *m_array;
    size_t m_capacity = N * sizeof(T); // Capacity of the array
    size_t m_size; // Number of added elements
public:
    const int GROWTH_FACTOR = 2;

    DynamicArray(std::initializer_list<T> elements) : m_size(elements.size()) {
        m_array = new T[m_capacity / sizeof(T)];
        std::copy(elements.begin(), elements.end(), m_array);
    }

    DynamicArray() : m_size(0) {
        m_array = new T[m_capacity / sizeof(T)];
    }
    
    void resize() {
        T *temp = new T[m_capacity / sizeof(T) * GROWTH_FACTOR];
        std::copy(m_array, m_capacity / sizeof(T) + m_array, temp);
        m_array = temp;
        delete[] temp;
    }

    void print() {
        for (int i = 0; i < m_size; i++){
            std::cout << m_array[i] << " ";
        }
        std::cout << std::endl;
    }
    

};

int main() {
    DynamicArray<int, 5> array = { 1, 22, 3, 4 };
    array.print();
    array.resize();
    array.print();
    return 0;
}

Output:

1 22 3 4 
0 0 3 4 
Geno C
  • 1,401
  • 3
  • 11
  • 26
Pythenx
  • 72
  • 6
  • 1
    Not causing you a problem yet but you need to implement the [rule of 3](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – Alan Birtles Jul 18 '20 at 07:02
  • 1
    The setting of the initial capacity as a template parameter is a bit strange. Especially as the capacity can change over the lifetime of the array. A capacity is not realy a part of an dynamic arrays type, so I don't think it should be a template parameter. Just make it a constant like `GROWTH_FACTOR` or a constructor parameter if you want the user to be able to set it. – john Jul 18 '20 at 07:06

1 Answers1

2

You are deleteing the memory you just allocated. Resize should work by allocating new memory copying elements from the old memory and then deleteing the old.

Something like this

void resize() {
    T *temp = new T[m_capacity / sizeof(T) * GROWTH_FACTOR];
    std::copy(m_array, m_capacity / sizeof(T) + m_array, temp);
    delete[] m_array;
    m_array = temp;
}

I would also look at how you multiply the capacity by sizeof(T) and then everywhere that you use it, you divide the capacity by sizeof(T). If you eliminate both operations you'll get the same result.

john
  • 85,011
  • 4
  • 57
  • 81
  • 1
    Additionally, you should write a destructor to clean up the array as well: DynamicArray::~DynamicArray() { delete[] m_array; } – Dwight Jul 18 '20 at 07:01
  • 2
    Yes, your class also needs a destructor, a copy constructor, an copy assignment operator, and preferably a move constructor and a move assignment operator. But I guess it's just work in progress. Doing all of this is definitely a good learning tool. – john Jul 18 '20 at 07:03
  • Then should m_capacity and m_size should be ints? I read somewhere that they should be size_t, but that just makes a big mess in code – Pythenx Jul 18 '20 at 07:05
  • 1
    In the standard library they are `size_t` but in my view there's nothing wrong with having them as `int` if that's the interface you prefer. – john Jul 18 '20 at 07:09