I have a project in my programming class. In the project I need to create a dynamically allocated array in a template class. One of the things the class needs to do is be able to add and subtract elemtents within it, and adjust the size of the array to fit the changes. The size of the array is incremented or decremented by a "CHUNK" size, all this is is a global constant. For instance, if I wanted to create a template array and populate it with 5 integers, given a chunk size of three, I need to be able to fill the template array and increase its size dynamically to fit those elements.
The function that takes care of this is called "add". The add function adds one element at a time to the array and need to be able to increase the size of the array if the array is not big enough to add it. The barebones of my program looks like this:
#include <iostream>
usingnamespace std;
constint CHUNK = 3;
template < class T> class RubberArray
{
private:
T* _array; //the template array
unsigned _length; //the number of element in the array
public:
RubberArray ()
{
_array = new T[CHUNK];
_length = 0;
}
RubberArray( const RubberArray& n)
{
_array = n._array;
_length = n._length;
}
~RubberArray( )
{
delete[] _array;
}
void add ( const T& n)
{
if (_length%CHUNK == 0 && _length != 0)//checks that the array is not full. If the array is full _length will be a multiple of CHUNK and the % will be equal to 0
{
T* temp = new T[_length + CHUNK];//create temp array of the appropriate size
for (int i = 0; i < _length; i++)
{
temp[i] = _array[i];//member by member copy
}
_array = temp;//set data member equal to the temp array and delete it.
delete[] temp;
}
_array[_length++] = n;
};
friend ostream& operator<< ( ostream& os, const RubberArray& n)
{
for (int i = 0; i < n._length; i++)
{
os << n._array[i] << ", ";
}
return os;
}
int main()
{
RubberArray <int> i(0);
i.add(1);
i.add(2);
i.add(3);
i.add(4);
cout << i << endl //
}
The output looks something like this. I am coding in a linux environment for reference:
EDIT: Both are pointers to arrays, so I am pretty confused as to wether making _array = temp makes _array and alias for the pointer temp, or an alias for the data pointed to by temp. Its all very confusing.
If you do this _array = temp;
you make the pointer _array point to some memory, yes? The same memory that temp points to.
So if you then delete[] temp;
you are instructing that you no longer care about the memory temp points to... which is the same thing array points to! But you do care about that memory - you're using it as your replacement, bigger array.
Ok, thanks I will. One questions, can I delete the pointer "temp" as well? Or will that not be neccessary, because it will fall out of scope as soon as the function returns?
delete does not act on the pointer. delete acts on the memory the pointer is pointing to. When you "delete a pointer", the pointer remains in existence; all it does is mark the memory the pointer is pointing to as no longer being memory that you care about. That's it. The pointer does not in any way get removed.
At the end of the add function, is the pointer temp pointing at memory that you still care about?
I haven't checked. I am pretty sure it will fall out of scope because I created it locally within add, so I shouldn't need to worry about it. I am just new to dynamic memory and wanted to be thorough. In any event, that fix worked! I am no longer having memory issues and the output looks correct. Thank you so much.