使用strdup实现的C语言图结构内存释放失败问题求助
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
datafield: You’re usingmalloc(and likelystrduplater) to assign strings here—this lives on the heap and needs explicit freeing. - Vertex
gccfield: Even though you initialize it toNULL, if you later allocate memory for it (e.g., withstrdupormalloc), you’ll need to free that too. - Edge nodes (
ENode): The adjacency list for each vertex is a linked list of dynamically allocatedENodeinstances—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
verticesarray—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.
strdupusesmalloc: Remember thatstrdupdynamically allocates memory under the hood, so freeing it withfree()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

