I have a need to allocate X amount of structs that will need to be stored for later and I am confused about allocations using .alloc() method.
Here is example of code that I would like to understand, using ArenaAllocator:
const boxes = try self.boxAllocator.allocator().alloc(Box, items.len);
defer self.boxAllocator.allocator().free(boxes);
Questions I have:
- Is this a correct pattern to create X amout of items?
- does
.freereturns memory of slice structure and individualBoxitems inside slice? (basically takes care of both) - since I may need items in slice for later, do I have to apply
.freeoperation in same scope? how do I free the slice, but not the items in slice?
My confusion stems from fact that I see slice and items inside the slice as two separate data structures that need memory, hence I would appreciate clarification about should I even care about slice as separate construct?