krahets/hello-algo · error
Vertex index out of bounds
Error message
Vertex index out of bounds
What it means
Emitted by removeVertex() when the supplied index is outside [0, graph->size). Because the graph stores vertices in a contiguous array indexed by position (not by value), an out-of-range index has no meaning and is rejected. The function logs to stderr and returns void without modifying the graph, so the caller has no programmatic signal of failure.
Solutions
- Guard the call: verify 0 <= index && index < graph->size before invoking removeVertex.
- After each deletion, recompute any cached indices — removeVertex shifts all vertices above 'index' down by one.
- If you only know the vertex value, scan graph->vertices[0..size-1] to find its current index before removing.
Example fix
// before removeVertex(graph, 5); // graph->size == 5 -> out of bounds // after if (graph->size > 0) removeVertex(graph, graph->size - 1);
Defensive patterns
Strategy: validation
Validate before calling
static inline int graphValidIndex(const GraphAdjMat *g, int idx) {
return g != NULL && idx >= 0 && idx < g->size;
}
/* usage */
if (graphValidIndex(graph, index)) {
removeVertex(graph, index);
} Prevention
- Never cache vertex indices across mutations — re-derive them from vertices[] each time.
- Name variables to distinguish values from indices (vertexVal vs vertexIdx).
- Remember removeVertex shifts every vertex with index > 'index' down by one.
When it happens
Trigger: removeVertex(graph, index) with index < 0 or index >= graph->size. Examples: removeVertex(graph, 5) on a 5-vertex graph (valid indices are 0..4); removeVertex(graph, 3) using a stale index after an earlier deletion shifted vertices left.
Common situations: Caching a vertex index across mutations — removeVertex shifts every higher-indexed vertex down by one, so old indices go stale; confusing a vertex's stored value with its array position; using a loop counter as an index while deleting inside the loop.
Related errors
- 頂点インデックスが範囲外です
- индекс вершины выходит за границы
- 頂點索引越界
- Edge index out of bounds or equal
- индексы ребра выходят за границы или совпадают
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/2229aae422edbc2f.
Report an issue: GitHub.
Appendix: source
Thrown at en/codes/c/chapter_graph/graph_adjacency_matrix.c:54
/* Add vertex */
void addVertex(GraphAdjMat *graph, int val) {
if (graph->size == MAX_SIZE) {
fprintf(stderr, "Graph vertex count has reached maximum\n");
return;
}
// Add nth vertex and zero nth row and column
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++;
}
/* Remove vertex */
void removeVertex(GraphAdjMat *graph, int index) {
if (index < 0 || index >= graph->size) {
fprintf(stderr, "Vertex index out of bounds\n");
return;
}
// Remove the vertex at index from the vertex list
for (int i = index; i < graph->size - 1; i++) {
graph->vertices[i] = graph->vertices[i + 1];
}
// Remove the row at index from the adjacency matrix
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];
}
}
// Remove the column at index from the adjacency matrix
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)