2 ms·
> And if you are not using arena -- realloc() is your best bet. And no, it won't move your memory around into larger areas every time you call it. Most of the t
by lelanthran 3y ago
> And if you are not using arena -- realloc() is your best bet. And no, it won't move your memory around into larger areas every time you call it. Most of the time it will simply bump up the allocation end point. This happens more frequently than you think.
And, as far as I can tell on the most common implementations, `realloc` just does a pointer adjustment when you shrink[1] the array, so the next `realloc` to expand the array again simply does the pointer bump thing.
Over time[2], you effectively get a arena allocator from that single realloc()ed block just by expanding and shrinking the array.
[1] Just don't call `realloc (0, ...)`
[2] For my next project I'm going to make an arena-allocator interface that is just a thin wrapper around realloc.