4 ms·
Carrying the allocator everywhere does introduce a new class of bugs — if you call into a library at a later point in time to interact with something that was p
by eqvinox 3y ago
Carrying the allocator everywhere does introduce a new class of bugs — if you call into a library at a later point in time to interact with something that was previously allocated, but you pass a different allocator this time… how exactly does that work?
(As opposed to: set up the allocator once, in an initialization call.)
- flohofwoe 3y agoZig stdlib "objects" like dynamic containers indeed usually only take the allocator during initialization and then store it in the instance. E.g. var list = ArrayList(u8).init(test_allocator); defer list.deinit(); try list.append('H'); try list.append('e'); ...
- Cloudef 3y agoThere are also often Unmanaged variants where you pass allocator to every method. In these scenarios you have to make sure you use the same allocator though. Unmanaged can be useful for short lived allocations or type that combines many standard containers, saving memory.