{"record":{"id":"c09d1e6cfe3390de","repo":"krahets/hello-algo","slug":"error-c09d1e","errorCode":null,"errorMessage":"スタックが空です","messagePattern":"スタックが空です","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ja/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/ja/codes/zig/chapter_stack_and_queue/array_stack.zig#L22-L58","documentation":"This panic is raised by peek() in an array-backed stack built on std.ArrayList (Japanese localization). It fires when reading items[size-1] while the stack is empty (size 0), which would also underflow the index. The guard prevents both a meaningless read and an unsigned underflow. @panic is uncatchable in Zig.","triggerScenarios":"Calling stack.peek() when stack.?.items.len == 0; note pop() here does NOT call peek() (it uses ArrayList.pop directly), so this panic is specific to peek().","commonSituations":"Reading the top to decide precedence in an empty operator stack; checking the top before the first push; iterating until empty then peeking once more.","solutions":["Guard with stack.isEmpty() before peek().","Cache stack.size() and only peek when > 0.","Loop with while (!stack.isEmpty()) and peek inside.","Be aware pop() on this implementation does not guard — also check before popping."],"exampleFix":"// before\nvar top = stack.peek(); // @panics when empty\n\n// after\nif (stack.isEmpty()) return;\nvar top = stack.peek();","handlingStrategy":"validation","validationCode":"// Validate non-empty before reading the top\nif (!stack.isEmpty()) {\n    var top = stack.peek();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["On this impl pop() does NOT call peek() — but pop() is still unguarded, so check before popping too.","Use while (!stack.isEmpty()) and peek inside.","Remember @panic aborts; there is no try/catch in Zig."],"tags":["zig","stack","array-list","panic","precondition","data-structure","i18n-ja"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}