0

I am trying to add an array to my Object which contains dynamic array

MyCollection.h

    template <typename T>
    MyCollection<T>::MyCollection(int size)
    {
        arraySize = size;
        anArray = new T[size];
    }

    template <typename T>
    MyCollection<T>::MyCollection(MyCollection<T>& coll)
    {
        arraySize = coll.arraySize;
        anArray = new T[arraySize];

        for (int i = 0; i < arraySize; i++)
        {
            SetValue(coll.GetValue(i), i);
        }
    }

    template <typename T>
    MyCollection<T>::MyCollection(T* myArray, int size)
    {
        anArray = myArray;
        arraySize = size;
    }




   template<typename T>
    void MyCollection<T>::AddAll(T pArray[], int size)
    {
        int plusSize = size - 1;
        int arrayIterator = 0;
        arraySize += size;


        for (int i = size; i < arraySize - plusSize; i++)
        {
            anArray[i] = pArray[arrayIterator];
            arrayIterator++;
        }

    }

Main

MyCollection<string> B = MyCollection<string>(new string[3], 3);
    B.SetValue("C", 0);
    B.SetValue("D", 1);
    B.SetValue("E", 2);

string C[3];
    C[0] = "X";
    C[1] = "Y";
    C[2] = "Z";

B.AddAll(C, 3);
    B.Display();

In the AddAll method I have error with acces violation. When I add watch anArray[i] = . Is there any idea why that happens? Is it problem with copy construcotr or so ?

0

1 Answer 1

1

In AddAll(), you need to resize the raw array anArray, there is no resizing going on (you just change the numeric value of arraySize, but do not allocate more memory). So you need something like

delete[] anArray; 
// ...
anArray = new T[arraySize]; 

You may also think whether you want to copy the old elements into the newly allocated array, in which case you first have to reallocate with a temporary pointer, then copy into it, then delete the original anArray, and finally assign the temporary pointer to the original anArray, something like:

// save the old size
int oldSize = arraySize;
arraySize += size;
T* tmp = new T[arraySize];
// copy from anArray into tmp, use oldSize
// for(...){...}
delete[] anArray;
anArray = tmp;
Sign up to request clarification or add additional context in comments.

5 Comments

So now If i do it like anArray = new int[newsize] then I will delete elements or not ?
@miechooy You need first to explicitly delete the memory pointed by the old array, then reallocate. If you don't delete, you end up with a memory leak. See the updated edit.
T* tempArray[] = new T[arraySize]; for (int i = 0; i < arraySize; i++) { tempArray[i] = anArray[i]; } Cannot convert string to *string
@miechooy you need to save the old size, so you don't copy using the new size. Also, T* tempArray instead of T* tempArray[].
@miechooy Also, think about adding one member function that does only resizing, call it resize(). Then implementing AddAll will be a breeze, you just invoke resize() then add the new elements at the end. Functions should be small and should do in general only one task, and do it well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.