{"record":{"id":"b14c0a949a847b4b","repo":"krahets/hello-algo","slug":"error-b14c0a","errorCode":null,"errorMessage":"边索引越界或相等\n","messagePattern":"边索引越界或相等\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"codes/c/chapter_graph/graph_adjacency_matrix.c","lineNumber":80,"sourceCode":"    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    }\n    graph->size--;\n}\n\n/* 添加边 */\n// 参数 i, j 对应 vertices 元素索引\nvoid addEdge(GraphAdjMat *graph, int i, int j) {\n    if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n        fprintf(stderr, \"边索引越界或相等\\n\");\n        return;\n    }\n    graph->adjMat[i][j] = 1;\n    graph->adjMat[j][i] = 1;\n}\n\n/* 删除边 */\n// 参数 i, j 对应 vertices 元素索引\nvoid removeEdge(GraphAdjMat *graph, int i, int j) {\n    if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n        fprintf(stderr, \"边索引越界或相等\\n\");\n        return;\n    }\n    graph->adjMat[i][j] = 0;\n    graph->adjMat[j][i] = 0;\n}\n\n/* 打印邻接矩阵 */","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/codes/c/chapter_graph/graph_adjacency_matrix.c#L62-L98","documentation":"Printed to stderr by addEdge in the GraphAdjMat teaching class when either vertex index i or j is negative, >= graph->size, or when i == j (self-loops are rejected). The function prints and returns silently without modifying the adjacency matrix. Since this is an undirected graph, the check applies symmetrically to both endpoints.","triggerScenarios":"Calling addEdge with a vertex index that has not been created yet (>= size), passing negative values, or attempting to add a self-loop (i == j). Also triggered if vertices were removed and stale indices are reused.","commonSituations":"Passing vertex values instead of indices; adding an edge before both vertices exist (vertices must be added first); attempting self-loops which this implementation forbids; using stale indices after removeVertex shifted the vertex list.","solutions":["Ensure both vertices exist first: check 'i >= 0 && j >= 0 && i < graph->size && j < graph->size && i != j'.","Add both vertices via addVertex before adding the edge between them.","Avoid self-loops (i == j) — this implementation explicitly rejects them.","After removeVertex, recompute vertex indices since the list shifts."],"exampleFix":"// before\naddEdge(graph, 0, 5);  // fails if graph has fewer than 6 vertices\n\n// after\nif (i >= 0 && j >= 0 && i < graph->size && j < graph->size && i != j) {\n    addEdge(graph, i, j);\n}","handlingStrategy":"validation","validationCode":"if (i >= 0 && j >= 0 && i < graph->size && j < graph->size && i != j) {\n    addEdge(graph, i, j);\n}","typeGuard":"/* C: precondition check before addEdge */\nstatic inline bool valid_edge(const GraphAdjMat *g, int i, int j) {\n    return i >= 0 && j >= 0 && i < g->size && j < g->size && i != j;\n}","tryCatchPattern":null,"preventionTips":["C has no exceptions — validate both indices and i != j before calling.","Add both vertices via addVertex before adding an edge between them.","Self-loops (i == j) are explicitly rejected by this implementation.","Parameters are vertex INDICES, not values — do not confuse them."],"tags":["c","data-structures","graph","adjacency-matrix","bounds-check","silent-failure","stderr","self-loop"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}