{"record":{"id":"a6df4fd6b9441d89","repo":"krahets/hello-algo","slug":"error-a6df4f","errorCode":null,"errorMessage":"顶点索引越界\n","messagePattern":"顶点索引越界\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"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/codes/c/chapter_graph/graph_adjacency_matrix.c#L36-L72","documentation":"Printed to stderr by removeVertex in the GraphAdjMat teaching class when index < 0 or index >= graph->size. Like the other C graph functions, this prints to stderr and returns silently — no errno, no return code, no abort. The vertex is not removed, but the caller has no programmatic signal of failure beyond checking preconditions.","triggerScenarios":"Calling removeVertex(graph, negative_index), calling it with an index >= the current vertex count, or calling it on an empty graph (size 0) where no index is valid.","commonSituations":"Using a vertex value instead of its index (the API takes index, not value); off-by-one when iterating vertex indices; calling removeVertex after size has already decreased in a loop; removing from an empty graph.","solutions":["Check 'index >= 0 && index < graph->size' before calling removeVertex.","Remember that parameters are vertex indices, not vertex values — look up the index first.","When removing multiple vertices in a loop, iterate in reverse order so earlier indices stay valid.","Check graph->size > 0 before attempting any removal."],"exampleFix":"// before\nremoveVertex(graph, 99);  // fails if graph has fewer than 100 vertices\n\n// after\nif (index >= 0 && index < graph->size) {\n    removeVertex(graph, index);\n}","handlingStrategy":"validation","validationCode":"if (index >= 0 && index < graph->size) {\n    removeVertex(graph, index);\n}","typeGuard":"/* C: precondition check before removeVertex */\nstatic inline bool valid_vertex_index(const GraphAdjMat *g, int index) {\n    return index >= 0 && index < g->size;\n}","tryCatchPattern":null,"preventionTips":["C has no exceptions — validate 'index >= 0 && index < graph->size' before calling.","The function takes a vertex INDEX, not a vertex VALUE — look up the index first.","After removeVertex, vertex indices shift — re-derive them rather than caching.","The function returns void with no error code, so check preconditions yourself."],"tags":["c","data-structures","graph","adjacency-matrix","bounds-check","silent-failure","stderr"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}