Skip to content

Commit efc2fef

Browse files
committedOct 2, 2023
fix: remove memory leak in max_heap.c
1 parent e5dad3f commit efc2fef

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
 

‎data_structures/heap/max_heap.c

+18
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ typedef struct max_heap
1111

1212
Heap *create_heap(Heap *heap); /*Creates a max_heap structure and returns a
1313
pointer to the struct*/
14+
/**
15+
* @brief Deallocates memory associated with a given heap.
16+
*
17+
* @param heap Pointer to the heap structure to be deallocated.
18+
*/
19+
void delete_heap(Heap *const heap);
20+
1421
void down_heapify(Heap *heap, int index); /*Pushes an element downwards in the
1522
heap to find its correct position*/
1623
void up_heapify(Heap *heap, int index); /*Pushes an element upwards in the heap
@@ -46,6 +53,7 @@ int main()
4653
printf("Popping an element.\n");
4754
printf("Top element = %d \n", top(head));
4855
printf("\n");
56+
delete_heap(head);
4957
return 0;
5058
}
5159
Heap *create_heap(Heap *heap)
@@ -57,6 +65,16 @@ Heap *create_heap(Heap *heap)
5765
return heap;
5866
}
5967

68+
void delete_heap(Heap *const heap)
69+
{
70+
if (heap == NULL)
71+
{
72+
return;
73+
}
74+
free(heap->p);
75+
free(heap);
76+
}
77+
6078
void down_heapify(Heap *heap, int index)
6179
{
6280
if (index >= heap->count)

0 commit comments

Comments
 (0)
Failed to load comments.