#Why dont i need to free a owned slice?
1 messages · Page 1 of 1 (latest)
arraylist if it's expected that the caller might need to add or remove items, slice if not
the function that returns a slice often takes in an allocator, which in most cases indicates that the slice was allocated and needs to be deinit after done using it
Worth noting that toOwnedSlice might allocate a new buffer and copy elements over. For that reason, if you are looking for absolute best performance it might be better to return the array directly.
Would be nice if we had a DynamicBoundedArray to return instead.
how would such a thing work?
if the arraylist goes out of scope, so does the backing slice
You could return a type is essentially the same as an array list, but cannot use an allocator to grow the capacity
You can return an array list as long as you don't deinit it.
Zig doesn't have destructor , so doesn't need move semantics either
im aware
but u said return the array, which sounds to me like u means list.items
which, ig its allocated, so that could be fine
Oh no I meant the array list
No because you can't free that slice. The array list may have allocated a larger array, which it keeps track of internally.
So you need to return a type that stores a slice + the size of the allocation so it can be freed
right that makes sense
i couldnt remember
not at my computer rn to check the impl
Yeah array list is a bit weird in it's impl but it let's you query items.len or iterate over items that way
yeah
is this different than ArrayListUnmanaged?
what's the benefit in (i assume) allowing the caller to free the underlying slice with an allocator, but not to resize it?
No you are right, it's no different
as long as you free the returned slice (and free memory owned by each item, if there is any) you're fine
toOwnedSlice leaves the arraylist empty, so calling deinit is safe but unnecessary
ToOwnedSlice may allocate and copy, so yeah it's not free.