3 ms·
After some thought and experimentation, I have arraived at this interface: void *resize_array(void *old, size_t existing_size,
by kazinator 12y ago
After some thought and experimentation, I have arraived at this interface:
void *resize_array(void *old,
size_t existing_size,
size_t new_size
size_t resize_factor,
size_t elsize);
The client code computes the new size, while retaining the old. If these are supposed to be related by an integer factor, then this is passed as resize_factor. If they are not related that way, then resize_factor is passed in as zero. The elsize parameter must be nonzero, in any case.
If a nonzero resize_factor is supplied, then the calculation new_size = existing_size * resize_factor is checked for overflow, otherwise the zero value of resize_factor indicates that this check is not applicable; however, the function still at least validates that new_size > existing_size.
The calculation bytes = new_size * elsize is always checked for overflow.