How does arraylist deinitialization work?

The Magic of the Empty Array Literal

The line items = &[_]T{} creates a compile-time known empty array. This is special because:

  1. It has no runtime allocation - it’s essentially a zero-sized type
  2. It points to read-only memory (typically in the .rodata section)
  3. Zig knows not to free compile-time known memory

i.e.

pub fn deinit(self: *ArrayList) void {
    if (self.allocatedSlice) |slice| {
        self.allocator.free(slice);
    }
}