You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用strdup实现的C语言图结构内存释放失败问题求助

Fixing Memory Deallocation Issues in Your C Graph Implementation

Alright, let's tackle that memory leak problem you're facing with your adjacency list graph. From your code snippets, it looks like you're dynamically allocating memory for vertex data (using malloc or strdup) but not properly cleaning up all the allocated resources. Let's break down exactly what needs to be freed and how to do it step by step.

Key Allocated Resources to Track

First, let's map out all the heap-allocated memory in your structure:

  • Vertex data field: You’re using malloc (and likely strdup later) to assign strings here—this lives on the heap and needs explicit freeing.
  • Vertex gcc field: Even though you initialize it to NULL, if you later allocate memory for it (e.g., with strdup or malloc), you’ll need to free that too.
  • Edge nodes (ENode): The adjacency list for each vertex is a linked list of dynamically allocated ENode instances—these are easy to forget to free and a common source of leaks.

Step-by-Step Cleanup Implementation

First, create a helper function to free the linked list of edge nodes for a single vertex:

void free_edge_list(ENode *head) {
    ENode *temp_node;
    while (head != NULL) {
        temp_node = head;
        head = head->nextarc;
        free(temp_node); // Free the current edge node
    }
}

Next, create a function to clean up the entire graph. You’ll need to know how many vertices you actually initialized (since your vertices array is fixed-size at 100, but you probably aren’t using all of them):

void free_graph(Graph *graph, int vertex_count) {
    // Iterate over every initialized vertex
    for (int i = 0; i < vertex_count; i++) {
        // Free the vertex's data string
        if (graph->vertices[i].data != NULL) {
            free(graph->vertices[i].data);
            graph->vertices[i].data = NULL; // Nullify to avoid dangling pointers
        }
        // Free the gcc string if it was allocated later
        if (graph->vertices[i].gcc != NULL) {
            free(graph->vertices[i].gcc);
            graph->vertices[i].gcc = NULL;
        }
        // Free the entire adjacency list for this vertex
        free_edge_list(graph->vertices[i].firstarc);
        graph->vertices[i].firstarc = NULL;
    }
    // If you allocated the Graph itself with malloc (e.g., Graph *g = malloc(sizeof(Graph))),
    // uncomment this line to free the graph struct:
    // free(graph);
}

Critical Notes to Avoid Leaks

  • Track your vertex count: Never iterate over all 100 elements in the vertices array—only loop through the ones you actually initialized. If you don’t track this count, you’ll either miss freeing some vertices or access uninitialized memory (which causes undefined behavior).
  • Free in reverse order of allocation: Always free internal resources (like edge nodes and strings) before freeing the parent structure (if the graph itself is heap-allocated).
  • Nullify pointers after free: This prevents accidental use of dangling pointers later in your code.
  • strdup uses malloc: Remember that strdup dynamically allocates memory under the hood, so freeing it with free() is exactly the right approach—no special handling needed.

Example Usage

If you initialized, say, 5 vertices in your graph, you’d call the cleanup like this:

// After using your graph...
free_graph(my_graph, 5);

内容的提问来源于stack exchange,提问作者Turk Hussein

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:53:45