Skip to content

Commit

Permalink
Pass the valgrind test
Browse files Browse the repository at this point in the history
  • Loading branch information
paul90317 committed Mar 31, 2024
1 parent a2b6872 commit dd25b20
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ element_t *q_remove_head(struct list_head *head, char *sp, size_t bufsize)
element_t *element = container_of(head->next, element_t, list);
list_del(&element->list);
if (element->value && sp) {
memcpy(sp, element->value, bufsize - 1);
strncpy(sp, element->value, bufsize - 1);
sp[bufsize - 1] = 0;
}
return element;
Expand All @@ -89,7 +89,7 @@ element_t *q_remove_tail(struct list_head *head, char *sp, size_t bufsize)
element_t *element = container_of(head->prev, element_t, list);
list_del(&element->list);
if (element->value && sp) {
memcpy(sp, element->value, bufsize - 1);
strncpy(sp, element->value, bufsize - 1);
sp[bufsize - 1] = 0;
}
return element;
Expand Down Expand Up @@ -228,7 +228,7 @@ static void merge_tail_init(list_t *list, list_t *head)
void q_sort(struct list_head *head, bool descend)
{
int stack_cap = 33 - __builtin_clz(q_size(head) - 1);
list_t *stack = calloc(stack_cap, sizeof(list_t));
list_t stack[33];
for (int i = 0; i < stack_cap; ++i) {
INIT_LIST_HEAD(&stack[i].list);
stack[i].size = 0;
Expand Down Expand Up @@ -339,7 +339,7 @@ int q_merge(struct list_head *head, bool descend)
{
// https://leetcode.com/problems/merge-k-sorted-lists/
int heap_size = q_size(head);
list_t *heap = calloc(heap_size, sizeof(list_t));
list_t heap[100];

struct list_head *next = head->next;
for (int i = 0; i < heap_size; ++i) {
Expand Down

0 comments on commit dd25b20

Please sign in to comment.