I am trying to create a dynamically allocated array, that when filled, creates a new array large enough to hold everything and copies all the values from the old array to the new array. I do in my append function, which when called dynamically allocates a new array with tmp pointing to it, and then moves the values from arr[i] to tmp [i]. However, I'm wondering if I need to delete tmp when I'm done with it? Because when I try to print the contents of the array without deleting tmp, it prints just fine, but when I do delete tmp, things just start getting weird and the contents no longer print the way they should. Here's my code:
ArrayList::ArrayList(int initsize): last(0), size(initsize), origsize(initsize)
{
arr = new int[size];
}
ArrayList::ArrayList()
{
last = 0;
size = 16;
origsize = 16;
arr = new int[size];
}
void ArrayList::append(int value)
{
if (last<size) {
arr[last] = value;
}
last++;
if (last == size) {
int* tmp = new int[size+origsize];
int i = 0;
for (i=0; i<(size+origsize); i++)
tmp[i] = arr[i];
size = size+origsize;
arr = tmp;
arr[last] = value;
//delete tmp;
}
}
tmp, but I think you should deletearrright before thearr=tmpline.std::vector<int>is out of the question.