Skip to content

Commit 9d291a0

Browse files
committed
fix: remove memory leaks
1 parent e5dad3f commit 9d291a0

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

data_structures/graphs/kruskal.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ struct Graph *createGraph(int V, int E)
3636
return graph;
3737
}
3838

39+
/**
40+
* @brief Deallocates memory associated with a given graph.
41+
*
42+
* @param ptr Pointer to the graph structure to be deallocated.
43+
*/
44+
void deleteGraph(struct Graph *graph)
45+
{
46+
if (graph == NULL)
47+
{
48+
return;
49+
}
50+
free(graph->edge);
51+
free(graph);
52+
}
53+
3954
// A structure to represent a subset for union-find
4055
struct subset
4156
{
@@ -131,6 +146,7 @@ void KruskalMST(struct Graph *graph)
131146
}
132147
// Else discard the next_edge
133148
}
149+
free(subsets);
134150

135151
// print the contents of result[] to display the
136152
// built MST
@@ -182,6 +198,7 @@ int main()
182198
graph->edge[4].weight = 4;
183199

184200
KruskalMST(graph);
201+
deleteGraph(graph);
185202

186203
return 0;
187204
}

0 commit comments

Comments
 (0)