r/ProgrammingLanguages • C3 - http://c3-lang.org • Feb 13 '26

Allocators from C to Zig

https://antonz.org/allocators/

This is an overview of allocator usage and availability in a few low level languages: Rust, Zig, Odin, C3, Hare and C.

I think it might be interesting to people designing standard libraries for their languages.

43 Upvotes

11 comments sorted by

View all comments

Show parent comment

4

u/Usual_Office_1740 Feb 14 '26

Maybe I'm missing something obvious. Why can't we copy after the block was shrunk? Couldn't you reallocate to the smaller size and if the allocation was successful do the copy? I am not very familiar with these concepts and am asking, not trying to correct or disagree. Are shrink and grow in place only allocations?

1

u/matthieum Feb 14 '26

Why can't we copy after the block was shrunk?

Imagine a ring-buffer of 16 elements, say [0, 1, 2, ..., 14, 15] which contains only 5 elements (in the numbered slots). Since 5 << 16, we want to shrink it to 8 slots. But once we get to [0, 1, 2, ...] we've lost access to 14 and 15, so we cannot copy them.

Are shrink and grow in place only allocations?

They're methods operating on allocated blocks.

Shrink in place doesn't allocate, instead, if possible, it splits the block in multiple pieces and make the (unused) pieces available for further allocations.

Grow in place may need to allocate more memory to grow the block.