Hacker News new | ask | show | jobs
by rurban 47 days ago
You don't, as the capacity is always the next power of 2 of the length.
1 comments

If 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).