logoalt Hacker News

MangoToupeyesterday at 3:30 PM2 repliesview on HN

> As another commenter wrote "how do you allocate memory without an allocator?"

You call these things something other than an "allocating" and "allocators". Seriously, few people would consider adding a value to a hashmap an intentionally allocational activity, but it is. Same with adding an element to a vector, or any of the dependent actions on it.

Seriously


Replies

miningapetoday at 2:21 AM

Yep exactly, at it's simplest all an allocator is doing is keeping track of which memory areas are owned and their addresses. In a sense even the stack pointer is a kind of allocator (you can assign the memory pointed to by the stack pointer and then increment it for the next use)

tialaramexyesterday at 5:53 PM

For "adding an element to a vector" it's actually not necessarily what you meant here and in some contexts it makes sense to be explicit about whether to allocate.

Rust's Vec::push may grow the Vec, so it has amortized O(1) and might allocate.

However Vec::push_within_capacity never grows the Vec, it is unamortized O(1) and never allocates. If our value wouldn't fit we get the value back instead.