Skip to content

Comment on A generic dynamic array in C that stores no capacity and needs no struct

Comments

Strictly speaking, the capacity is still stored internally to the allocation (it needs to be, in order to implement realloc)

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

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.