krahets/hello-algo · warning

顶点索引越界

Error message

顶点索引越界

What it means

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.

Source

Thrown at codes/c/chapter_graph/graph_adjacency_matrix.c:54

/* 添加顶点 */
void addVertex(GraphAdjMat *graph, int val) {
    if (graph->size == MAX_SIZE) {
        fprintf(stderr, "图的顶点数量已达最大值\n");
        return;
    }
    // 添加第 n 个顶点,并将第 n 行和列置零
    int n = graph->size;
    graph->vertices[n] = val;
    for (int i = 0; i <= n; i++) {
        graph->adjMat[n][i] = graph->adjMat[i][n] = 0;
    }
    graph->size++;
}

/* 删除顶点 */
void removeVertex(GraphAdjMat *graph, int index) {
    if (index < 0 || index >= graph->size) {
        fprintf(stderr, "顶点索引越界\n");
        return;
    }
    // 在顶点列表中移除索引 index 的顶点
    for (int i = index; i < graph->size - 1; i++) {
        graph->vertices[i] = graph->vertices[i + 1];
    }
    // 在邻接矩阵中删除索引 index 的行
    for (int i = index; i < graph->size - 1; i++) {
        for (int j = 0; j < graph->size; j++) {
            graph->adjMat[i][j] = graph->adjMat[i + 1][j];
        }
    }
    // 在邻接矩阵中删除索引 index 的列
    for (int i = 0; i < graph->size; i++) {
        for (int j = index; j < graph->size - 1; j++) {
            graph->adjMat[i][j] = graph->adjMat[i][j + 1];
        }
    }

View on GitHub (pinned to 69932aed18)

Solutions

  1. Check 'index >= 0 && index < graph->size' before calling removeVertex.
  2. Remember that parameters are vertex indices, not vertex values — look up the index first.
  3. When removing multiple vertices in a loop, iterate in reverse order so earlier indices stay valid.
  4. Check graph->size > 0 before attempting any removal.

Example fix

// before
removeVertex(graph, 99);  // fails if graph has fewer than 100 vertices

// after
if (index >= 0 && index < graph->size) {
    removeVertex(graph, index);
}
Defensive patterns

Strategy: validation

Validate before calling

if (index >= 0 && index < graph->size) {
    removeVertex(graph, index);
}

Type guard

/* C: precondition check before removeVertex */
static inline bool valid_vertex_index(const GraphAdjMat *g, int index) {
    return index >= 0 && index < g->size;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13). Data as JSON: /api/errors/a6df4fd6b9441d89. Report an issue: GitHub.