HeadlinesBriefing favicon HeadlinesBriefing.com

Zeigerstabilität in Zig ArrayLists 2026 - Leitfaden

Hacker News •
×

This page contains a curated list of recent changes to the main branch of Zig for the year 2

Let's explore Pointer Stability for Array Lists. August 27, 2026 Pointer Stability for Array Lists Author: Robbie Lyman Pointer Stability Locks were added to std's Hash Map containers in 2024. A pull request initially opened by Leo Emar-Kar in 2025 now brings this technique for ensuring memory safety to std. Array List. To make use of this in your code, add a call to lock Pointers() when you first store a pointer to an element or a slice of elements backed by the Array List, and call unlock Pointers() when those pointers are no longer needed. Did you spot the bug? The problem is that elements of Context.lines.items depend on the location of Context.history.items, but this location may change if Context.history needs to grow beyond its current capacity. If elements are stored after the first call to parse in our test, what happens if we make this change? try ctx.parse(gpa, input);+ ctx.history.lock Pointers();+ defer ctx.history.unlock Pointers();try ctx.parse(gpa, input_two); We get a panic with a stack trace that shows us where our assumption about pointer stability was violated!thread 3023222 panic: reached unreachable code/Users/robbie/bin/lib/std/debug.zig:442:14: 0x102d2506f in assert (test)if (!ok) unreachable; // assertion failure^/Users/robbie/bin/lib/std/debug.zig:1880:15: 0x102d31ef7 in assert Unlocked (test)assert(l.state == .unlocked);^/Users/robbie/bin/lib/std/array_list.zig:1348:50: 0x102e3ced7 in ensure Total Capacity Precise (test)self.pointer_stability.assert Unlocked();^/Users/robbie/bin/lib/std/array_list.zig:1341:51: 0x102e3cdff in ensure Total Capacity (test)return self.ensure Total Capacity Precise(gpa, grow Capacity(new_capacity));^/Users/robbie/bin/lib/std/array_list.zig:1237:41: 0x102e4e5c3 in resize (test)try self.ensure Total Capacity(gpa, new_len);^/Users/robbie/bin/lib/std/array_list.zig:1461:28: 0x102e4e40f in add Many As Slice (test)try self.resize(gpa, try add Or Oom(self.items.len, n));^/Use...