Ex34: Bug in DArray_expand()?

I made my own version of a dynamic array that copies every value to the heap automatically (no idea if that’s a good idea…). While debugging my destroy and clear functions I may have found a little bug in the original code:

int DArray_expand(DArray *array)
{
    size_t old_max = array->max;
    ... // call DArray_resize
    memset(array->contents + old_max, 0, array->expand_rate + 1);
    return 0;
}

memset actually needs a number of bytes as its third parameter, but here we’re passing the number of elements that we want to be NULL. This causes uninitialized memory areas in array->contents, which, in turn, cause segfaults when we try to automatically free the addresses of values if the pointers are not NULL.

I think the third parameter must be (array->expand_rate + 1) * sizeof(void *). I did the math a little differently, but in principle that’s how I got it working.

Yessssss, I think you are correct in this analysis. Make the change and see if that’s right.