From 9d291a0e829260cf9dacb3289caeb42dc10fbe81 Mon Sep 17 00:00:00 2001 From: vil02 Date: Thu, 14 Sep 2023 18:20:23 +0200 Subject: [PATCH 1/2] fix: remove memory leaks --- data_structures/graphs/kruskal.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/data_structures/graphs/kruskal.c b/data_structures/graphs/kruskal.c index 0f72c6b52f..cd2b10a0b4 100644 --- a/data_structures/graphs/kruskal.c +++ b/data_structures/graphs/kruskal.c @@ -36,6 +36,21 @@ struct Graph *createGraph(int V, int E) return graph; } +/** + * @brief Deallocates memory associated with a given graph. + * + * @param ptr Pointer to the graph structure to be deallocated. + */ +void deleteGraph(struct Graph *graph) +{ + if (graph == NULL) + { + return; + } + free(graph->edge); + free(graph); +} + // A structure to represent a subset for union-find struct subset { @@ -131,6 +146,7 @@ void KruskalMST(struct Graph *graph) } // Else discard the next_edge } + free(subsets); // print the contents of result[] to display the // built MST @@ -182,6 +198,7 @@ int main() graph->edge[4].weight = 4; KruskalMST(graph); + deleteGraph(graph); return 0; } From 221a0578a4d8a2f027bd2b0d348d00ff7402e319 Mon Sep 17 00:00:00 2001 From: vil02 Date: Thu, 14 Sep 2023 20:05:02 +0200 Subject: [PATCH 2/2] style: add missing `const` --- data_structures/graphs/kruskal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/graphs/kruskal.c b/data_structures/graphs/kruskal.c index cd2b10a0b4..44844c0d7c 100644 --- a/data_structures/graphs/kruskal.c +++ b/data_structures/graphs/kruskal.c @@ -41,7 +41,7 @@ struct Graph *createGraph(int V, int E) * * @param ptr Pointer to the graph structure to be deallocated. */ -void deleteGraph(struct Graph *graph) +void deleteGraph(struct Graph *const graph) { if (graph == NULL) {