{"record":{"id":"03704c04fb96cf26","repo":"krahets/hello-algo","slug":"error-03704c","errorCode":null,"errorMessage":"栈为空","messagePattern":"栈为空","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"codes/zig/chapter_stack_and_queue/array_stack.zig","lineNumber":40,"sourceCode":"        // 析构方法（释放内存）\n        pub fn deinit(self: *Self) void {\n            if (self.stack == null) return;\n            self.stack.?.deinit();\n        }\n\n        // 获取栈的长度\n        pub fn size(self: *Self) usize {\n            return self.stack.?.items.len;\n        }\n\n        // 判断栈是否为空\n        pub fn isEmpty(self: *Self) bool {\n            return self.size() == 0;\n        }\n\n        // 访问栈顶元素\n        pub fn peek(self: *Self) T {\n            if (self.isEmpty()) @panic(\"栈为空\");\n            return self.stack.?.items[self.size() - 1];\n        }  \n\n        // 入栈\n        pub fn push(self: *Self, num: T) !void {\n            try self.stack.?.append(num);\n        } \n\n        // 出栈\n        pub fn pop(self: *Self) T {\n            var num = self.stack.?.pop();\n            return num;\n        } \n\n        // 返回 ArrayList\n        pub fn toList(self: *Self) std.ArrayList(T) {\n            return self.stack.?;\n        }","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/codes/zig/chapter_stack_and_queue/array_stack.zig#L22-L58","documentation":"@panic in peek() of the Zig ArrayStack when the stack is empty. peek() returns items[size-1], which underflows when size == 0, so it aborts with '栈为空' (stack is empty). Note that this stack's pop() does NOT route through peek() — it calls ArrayList.pop directly — so the documented '栈为空' panic is specific to peek(). @panic in Zig aborts the process and cannot be caught.","triggerScenarios":"Calling peek() on a freshly initialized stack with no pushes; peeking after popping every element; a read step that runs before any push in a startup sequence.","commonSituations":"Expression evaluators that peek an operator before any value is pushed; undo stacks inspected when empty; misordering initialization so inspection precedes the first push.","solutions":["Check isEmpty() before peek(); skip or return a sentinel/default when empty.","Use size() > 0 as the guard condition.","Ensure at least one push precedes the first peek in startup/order-dependent code.","Wrap peek() in a helper that returns ?T or an error instead of panicking."],"exampleFix":"// before: @panics '栈为空' on empty stack\nconst top = stack.peek();\n\n// after\nif (!stack.isEmpty()) {\n    const top = stack.peek();\n}","handlingStrategy":"validation","validationCode":"if (!stack.isEmpty()) {\n    const top = stack.peek();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Zig @panic is unrecoverable — guard with isEmpty() before peek().","Note pop() does not route through peek() here; the '栈为空' panic is peek-only.","Ensure at least one push precedes the first peek in startup code."],"tags":["zig","stack","empty-state","peek","validation"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}