{"record":{"id":"839d3e4e56bb37cd","repo":"krahets/hello-algo","slug":"error-839d3e","errorCode":null,"errorMessage":"頂點索引越界\n","messagePattern":"頂點索引越界\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"zh-hant/codes/c/chapter_graph/graph_adjacency_matrix.c","lineNumber":54,"sourceCode":"/* 新增頂點 */\nvoid addVertex(GraphAdjMat *graph, int val) {\n    if (graph->size == MAX_SIZE) {\n        fprintf(stderr, \"圖的頂點數量已達最大值\\n\");\n        return;\n    }\n    // 新增第 n 個頂點，並將第 n 行和列置零\n    int n = graph->size;\n    graph->vertices[n] = val;\n    for (int i = 0; i <= n; i++) {\n        graph->adjMat[n][i] = graph->adjMat[i][n] = 0;\n    }\n    graph->size++;\n}\n\n/* 刪除頂點 */\nvoid removeVertex(GraphAdjMat *graph, int index) {\n    if (index < 0 || index >= graph->size) {\n        fprintf(stderr, \"頂點索引越界\\n\");\n        return;\n    }\n    // 在頂點串列中移除索引 index 的頂點\n    for (int i = index; i < graph->size - 1; i++) {\n        graph->vertices[i] = graph->vertices[i + 1];\n    }\n    // 在鄰接矩陣中刪除索引 index 的行\n    for (int i = index; i < graph->size - 1; i++) {\n        for (int j = 0; j < graph->size; j++) {\n            graph->adjMat[i][j] = graph->adjMat[i + 1][j];\n        }\n    }\n    // 在鄰接矩陣中刪除索引 index 的列\n    for (int i = 0; i < graph->size; i++) {\n        for (int j = index; j < graph->size - 1; j++) {\n            graph->adjMat[i][j] = graph->adjMat[i][j + 1];\n        }\n    }","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/zh-hant/codes/c/chapter_graph/graph_adjacency_matrix.c#L36-L72","documentation":"Traditional-Chinese localization of the removeVertex bounds check (see error 601). Vertices are positional; an index outside [0, graph->size) is rejected with an stderr message and a void return carrying no status.","triggerScenarios":"removeVertex(graph, index) with index < 0 or index >= graph->size — usually a stale index after an earlier deletion.","commonSituations":"Stale cached indices across mutations; value/index confusion; in-loop deletion using a counter.","solutions":["Guard with 0 <= index && index < graph->size.","Recompute indices after each deletion.","Look up the index from vertices[] when you only have the value."],"exampleFix":"// before\nremoveVertex(graph, 5);   // size == 5\n\n// after\nif (graph->size > 0) removeVertex(graph, graph->size - 1);","handlingStrategy":"validation","validationCode":"static inline int graphValidIndex(const GraphAdjMat *g, int idx) {\n    return g != NULL && idx >= 0 && idx < g->size;\n}\n\nif (graphValidIndex(graph, index)) removeVertex(graph, index);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not cache indices across mutations; re-derive from vertices[].","Separate values from indices in naming.","Remember higher indices shift down after a deletion."],"tags":["graph","bounds","index","adjacency-matrix","c","i18n-zh-hant"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}