From f99e4941e4eefe2aea4f575e053f45bfd0e29385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Wed, 9 Aug 2023 20:25:03 +0200 Subject: [PATCH] fix: correct usage of malloc and sizeof for structs --- lib/linked_list.c | 6 +++--- lib/queue.c | 4 ++-- lib/stack.c | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/linked_list.c b/lib/linked_list.c index 032fcce..89741d7 100644 --- a/lib/linked_list.c +++ b/lib/linked_list.c @@ -1,7 +1,7 @@ #include "linked_list.h" struct linked_list *linked_list_initialization() { - struct linked_list *list = malloc(sizeof(*list)); + struct linked_list *list = malloc(sizeof(struct linked_list)); if (list == NULL) { perror("Error (linked_list_initialization)"); exit(EXIT_FAILURE); @@ -17,7 +17,7 @@ struct linked_list_node *linked_list_add_in_head(struct linked_list *list, void perror("Error (linked_list_add_in_head)"); exit(EXIT_FAILURE); } - struct linked_list_node *node_new = malloc(sizeof(*node_new)); + struct linked_list_node *node_new = malloc(sizeof(struct linked_list_node)); if (node_new == NULL) { perror("Error (linked_list_add_in_head)"); exit(EXIT_FAILURE); @@ -52,7 +52,7 @@ struct linked_list_node *linked_list_add_after_last(struct linked_list *list, vo if (list->head == NULL) { return linked_list_add_in_head(list, new_data); } - struct linked_list_node *node_new = malloc(sizeof(*node_new)); + struct linked_list_node *node_new = malloc(sizeof(struct linked_list_node)); if (node_new == NULL) { perror("Error (linked_list_add_after_last)"); exit(EXIT_FAILURE); diff --git a/lib/queue.c b/lib/queue.c index d353c1d..2b0dccb 100644 --- a/lib/queue.c +++ b/lib/queue.c @@ -1,7 +1,7 @@ #include "queue.h" struct queue *queue_initialization() { - struct queue *queue = malloc(sizeof(*queue)); + struct queue *queue = malloc(sizeof(struct queue)); if (queue == NULL) { perror("Error (queue_initialization)"); exit(EXIT_FAILURE); @@ -17,7 +17,7 @@ void queue_push(struct queue *queue, void *data) { perror("Error (queue_push)"); exit(EXIT_FAILURE); } - struct queue_node *node_new = malloc(sizeof(*node_new)); + struct queue_node *node_new = malloc(sizeof(struct queue_node)); if (node_new == NULL) { perror("Error (queue_push)"); exit(EXIT_FAILURE); diff --git a/lib/stack.c b/lib/stack.c index 87746eb..ecde934 100644 --- a/lib/stack.c +++ b/lib/stack.c @@ -1,7 +1,7 @@ #include "stack.h" struct stack *stack_initialization() { - struct stack *stack = malloc(sizeof(*stack)); + struct stack *stack = malloc(sizeof(struct stack)); if (stack == NULL) { perror("Error (stack_initialization)"); exit(EXIT_FAILURE); @@ -17,7 +17,7 @@ void stack_push(struct stack *stack, void *data) { perror("Error (stack_push)"); exit(EXIT_FAILURE); } - struct stack_node *node_new = malloc(sizeof(*node_new)); + struct stack_node *node_new = malloc(sizeof(struct stack_node)); if (data == NULL) { perror("Error (stack_push)"); exit(EXIT_FAILURE);