Since the array is using a POD (plain old data) type, you can replace the loops with memcpy()
instead:
unsigned _int8 *temp = new unsigned _int8[NewSize];
if (OldSize >= NewSize)
memcpy(temp, data, NewSize * sizeof(unsigned _int8));
else
{
memcpy(temp, data, OldSize);
memset(&temp[OldSize], 0, (NewSize-OldSize) * sizeof(unsigned _int8));
}
delete[] data;
data = temp;
Or at least use std::copy()
(for POD types, std::copy()
is like memcpy()
, but for non-POD types it uses loops so object assignment semantics are preserved):
unsigned _int8 *temp = new unsigned _int8[NewSize];
if (OldSize >= NewSize)
std::copy(data, &data[NewSize], temp);
else
{
std::copy(data, &data[OldSize], temp);
std::memset(&temp[OldSize], 0, (NewSize-OldSize) * sizeof(unsigned _int8));
}
delete[] data;
data = temp;
That being said, you really should use std::vector<unsigned _int8>
instead. It handles these details for you. This type of array management is what you have to use in C, but really should not use in C++ if you can avoid it, use native C++ functionality instead.