3 ms·
Strictly speaking, the capacity is still stored internally to the allocation (it needs to be, in order to implement realloc)
by dooglius 4mo ago
Strictly speaking, the capacity is still stored internally to the allocation (it needs to be, in order to implement realloc)
- rurban 4mo agoYou don't, as the capacity is always the next power of 2 of the length.
- dooglius 3mo agoIf you were to implement your own realloc for the purpose of this data structure, you could do that. However, a generic realloc implementation needs to know how many bytes to copy, and generally that means storing the capacity just in front of the allocated region. I don't think there is another feasible way. On e.g. glibc, you can use `malloc_usable_size` to extract this information (modulo certain caveats).