Skip to content

Commit

Permalink
fix: remove memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
vil02 committed Oct 2, 2023
1 parent e5dad3f commit 9d291a0
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions data_structures/graphs/kruskal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -182,6 +198,7 @@ int main()
graph->edge[4].weight = 4;

KruskalMST(graph);
deleteGraph(graph);

return 0;
}

0 comments on commit 9d291a0

Please sign in to comment.