Skip to content

Commit 057dd6d

Browse files
Gathrosberquist
authored andcommitted
Small change to utility.h (#500)
Fix memory leak in C tree traversal helper
1 parent 594b016 commit 057dd6d

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

contents/tree_traversal/code/c/utility.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,16 @@ void queue_resize(struct queue *q) {
7272
memcpy(tmp, q->data + q->front, (q->capacity - q->front) * size);
7373
memcpy(tmp + q->capacity - q->front, q->data, (q->front - 1) * size);
7474

75+
free(q->data);
76+
7577
q->data = tmp;
7678
q->back = q->capacity - 1;
7779
q->front = 0;
7880
q->capacity *= 2;
7981
}
8082

8183
void enqueue(struct queue *q, void *element) {
82-
if (q->front == (q->back % q->capacity) + 1) {
84+
if (q->front == (q->back + 1) % q->capacity) {
8385
queue_resize(q);
8486
}
8587

0 commit comments

Comments
 (0)