Here is my function that reads in a list of integers and doubles the size of the array if there are too many inputs. The current size of the array is 10.
int* read_list ( int *list, size_t *list_size_ptr, size_t *array_size ){
size_t index = 0;
int *new_list;
while ( cin >> list[index] ){
if ( index >= *array_size-1 ){
*array_size *= 2;
new_list = new int[*array_size];
int new_size = (int)(*array_size/2)*sizeof(int);
memcpy ( new_list, list, new_size );
list = new_list;
delete [] new_list;
}
index++;
}
*list_size_ptr = index;
return list;
}
This code works well for lists of size 8 or 16, but with sizes larger than 20 it outputs 0, 2, 0, 4, 0, 6, 0, 8, 9, 10, 11, ...
I'm just not sure why. I would think it would mess up like that the first time it reallocates a new size but it starts to do it the second time. Any ideas? Thanks.