If *count
is the number of elements in array
(used and unused) then this
array[*count+1] = array[*count];
will go beyond the boundaries of array
. The array
may be index from 0 to *count - 1.
If *count
is the number of used elements in array
, you need to look at the total size of array
before extending.
There are other indexes of array
that could be >= *count as well. Look at them all carefully.
If array is malloc'ed in the code that calls insertWord
you will need to realloc
it to resize array
.
In any event, seeing how array is created in the code that calls insertWord is needed to comment intelligently.
Ok, new info. Consider the situation where capacity=100, and count=99, you call insertWord and need to append the new word. This
array[*count+1] = array[*count];
becomes
array[100] = array[99];
That index of 100 is too big; capacity is 100, valid indices are 0-99. You can fix this by changing
if (*count >= *capacity) // double array
to
if (*count >= *capacity-1) // double array