Hi again,
I know that this is mainly for C, however it asks me to use it in my C++ book to deal with any overflow going into an array.
My idea was to have an initial array of size 10 and when reading into it, if the array went over 10 (kept track of by a counter), then I would use re-alloc to change the size of the array. As usual it hasn't worked out quite like I expected
Code:
istream& read(istream& is, int* s)
{
int temp;
int i = 0;
while(is>>temp && temp != '!'){
s[i] = temp;
++i;
if(i > 10) {
realloc(s, i*4);
}
}
return is;
}
Now, as I understand, the 2nd argument for realloc is the size in bytes, so I have set this as i*4 bytes, so if the array goes to size 11 then it should be 11 bytes. However, when I print the array, it is just random trash!
Any advice please?
Thanks.