Hacker News new | ask | show | jobs
by dooglius 2 days ago
Strictly speaking, the capacity is still stored internally to the allocation (it needs to be, in order to implement realloc)
1 comments

You don't, as the capacity is always the next power of 2 of the length.
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).