{"record":{"id":"e26a70c45dab27a4","repo":"krahets/hello-algo","slug":"error-e26a70","errorCode":null,"errorMessage":"图的顶点数量已达最大值\n","messagePattern":"图的顶点数量已达最大值\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"codes/c/chapter_graph/graph_adjacency_matrix.c","lineNumber":39,"sourceCode":"    GraphAdjMat *graph = (GraphAdjMat *)malloc(sizeof(GraphAdjMat));\n    graph->size = 0;\n    for (int i = 0; i < MAX_SIZE; i++) {\n        for (int j = 0; j < MAX_SIZE; j++) {\n            graph->adjMat[i][j] = 0;\n        }\n    }\n    return graph;\n}\n\n/* 析构函数 */\nvoid delGraphAdjMat(GraphAdjMat *graph) {\n    free(graph);\n}\n\n/* 添加顶点 */\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 的顶点","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/codes/c/chapter_graph/graph_adjacency_matrix.c#L21-L57","documentation":"Printed to stderr by addVertex in the GraphAdjMat teaching class (an adjacency-matrix undirected graph) when graph->size == MAX_SIZE (100). Unlike the Ruby implementations which raise exceptions, this C function merely prints the message and returns silently — it does NOT set errno, return an error code, or abort. The vertex is silently not added, so the caller has no programmatic way to detect the failure except by checking preconditions beforehand.","triggerScenarios":"Calling addVertex when the graph already contains exactly MAX_SIZE (100) vertices. This happens after 100 successful addVertex calls without any removeVertex calls, or if MAX_SIZE was lowered.","commonSituations":"Loading a graph dataset with more than 100 nodes; forgetting that MAX_SIZE is a compile-time #define (100); not calling removeVertex to free slots; increasing graph density without increasing MAX_SIZE.","solutions":["Check graph->size < MAX_SIZE before calling addVertex.","Increase the MAX_SIZE #define at the top of the file (or in common.h) and recompile if your dataset requires more vertices.","Call removeVertex to free a slot before adding a new vertex when at capacity.","Redirect stderr to a log file and scan for this message during testing."],"exampleFix":"// before\naddVertex(graph, val);  // silently fails if size == 100\n\n// after\nif (graph->size < MAX_SIZE) {\n    addVertex(graph, val);\n} else {\n    // handle capacity exhaustion: resize MAX_SIZE or remove a vertex\n}","handlingStrategy":"validation","validationCode":"if (graph->size < MAX_SIZE) {\n    addVertex(graph, val);\n}","typeGuard":"/* C: precondition check before addVertex */\nstatic inline bool can_add_vertex(const GraphAdjMat *g) {\n    return g->size < MAX_SIZE;\n}","tryCatchPattern":null,"preventionTips":["C has no exceptions — the only defense is checking graph->size < MAX_SIZE before the call.","The function returns void and does NOT set errno, so you cannot detect failure after the call.","Increase MAX_SIZE at compile time if your dataset needs more than 100 vertices.","Remove unused vertices with removeVertex to free slots before adding new ones."],"tags":["c","data-structures","graph","adjacency-matrix","capacity","silent-failure","stderr","fixed-size"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}