Consider using
size_tinstead ofint. The actual type of size_t is platform-dependent; a common mistake is to assume size_t is the same as unsigned int, which can lead to problems, particularly as 64-bit architectures become more prevalent.It is generally bad practice to repeat code that does the same thing. Consider changing
copyToNewSizeto something like this:
void copyToNewSize(size_t newSize) {
int* temp = new int[newSize];
size_t copySize = std::min(size, newSize);
for (size_t i = 0; i < copySize; i++) {
temp[i] = arr[i];
}
delete[] arr;
size = newSize;
arr = temp;
}
- I would suggest that you rename
sizetoallocatedSizeto avoid confusion betweensizeandlength.