I have a pointer to an array:
Code:
T *array = new T[capacity];
I want to double the array size when the array becomes to small. This is how I'm doing it, but its not working correctly. For some reason it gets rid of the first element:
Code:
template<typename T>
void ArrayStack<T>::doubleArraySize() {
capacity *= 2;
T *tempArray = array;
array = new T[capacity];
for (int i=0; i<=tos; i++) {
array[i] = tempArray[i];
}
array = tempArray;
delete[] tempArray;
}
Can anyone point out what I'm doing wrong?
When I print it should say:
3
2
1
instead:
3
2
some address