How to resize a dynamically allocated array inside a class?

  allocation, c++, class, resize

I have a private array inside a class, allocated dynamically. As I insert more and more items, I need to resize the array at some point. The question is how to do that correctly? The code below ends in a error: munmap_chunk(): invalid pointer while inserting a third item.

#include <string>
#include <cstring>
#include <cassert>
using namespace std;

template<typename T>
class Set
    T * array;
    size_t arraySize;
    unsigned int itemCount;


    Set() {
        arraySize = 1;
        itemCount = 0;
        array = new T[arraySize];

    bool Insert(const T item) {
        if (itemCount == arraySize) {
            T * tmpArray = new T[arraySize * 2];
            memcpy(tmpArray, array, arraySize * sizeof(T));
            arraySize *= 2;
            delete [] array;
            array = tmpArray;

        array[itemCount] = item;
        return true;


int main ()
    Set<string> x0;
    assert( x0 . Insert( "apple" ) );
    assert( x0 . Insert( "orange" ) );
    assert( x0 . Insert( "pineapple" ) );
    return 0;

I know I could use for example a vector to don’t care about the allocation, but I would like to know how to do that properly this way.

Please forgive, if the question is somehow inappropriate. It is my first time questioning

Source: Windows Questions C++