Hello,
I am reading a code, that is working, but I think it's wrong, and it should crash, but it does not. It merges Array1 into Array2 if there is enough space in Array2 to fit it, but first moves Array2 elements enough spaces to fit the elements from Array1.
Code:
struct array
{
int size;
int* data;
array(int s) : size(s) { data = new int[size]; }
};
// copy elements from src to dst
static void merge_array(const array* const src, array* dst)
{
if (dst->size < src->size) {
return; // cant do that - destinatin size is smaller than source
}
// move the elements of destination to the end
for(int i=0, j=dst->size; i < src->size; ++i, ++j) {
dst->data[j] = dst->data[i];
}
// append the source to the remaining elements
for(int i=0; i < src->size; ++i) {
dst->data[i] = src->data[i];
}
}
Why it is not crashing by the array out of index?