#question about lists and appending local scope data (General protection Exception)
1 messages · Page 1 of 1 (latest)
My guess, because is a common mistake, is that arena.allocator() creates an allocator on the stack (ie inside of init) and then when init finishes running, that allocator is destroyed, leaving the array without a valid allocator. (I’m on my phone so it’s possible I’m wrong here.)
Most of the time it’s recommend to use ArrayListUnmanaged and pass it the allocator stored in your struct (.alloc) instead of using ArrayList for this reason.
More precisely, arena.allocator() returns a value that contains (and thus holds on to) &arena -- which is a local variable, and so whos space is reused over time.
Well - it depends on what you're doing and why. There's a lot of options.
One option is to just not do so; to have every function that would use the allocator do self.arena.allocator() wherever it needs to, and remove the alloc field.
Another is to have pub fn init(self: *Arena, ...) and initialize in-place instead.
self.arena = .init(alloc.*)
(You don't actually need alloc: *std.mem.Allocator; alloc: std.mem.Allocator is fine
because Allocator already points to the state
)
o7
Allocator is a fat pointer, *Allocator is basically pointing to a pointer
also is this syntax real? .init(...) or just pseudo ish
It's decl literals.
Modern feature