1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-11-12 15:23:12 +01:00

fix: correct usage of malloc and sizeof for structs

This commit is contained in:
Théo LUDWIG 2023-08-09 20:25:03 +02:00
parent 6505e3ba49
commit f99e4941e4
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
3 changed files with 7 additions and 7 deletions

View File

@ -1,7 +1,7 @@
#include "linked_list.h" #include "linked_list.h"
struct linked_list *linked_list_initialization() { 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) { if (list == NULL) {
perror("Error (linked_list_initialization)"); perror("Error (linked_list_initialization)");
exit(EXIT_FAILURE); 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)"); perror("Error (linked_list_add_in_head)");
exit(EXIT_FAILURE); 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) { if (node_new == NULL) {
perror("Error (linked_list_add_in_head)"); perror("Error (linked_list_add_in_head)");
exit(EXIT_FAILURE); 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) { if (list->head == NULL) {
return linked_list_add_in_head(list, new_data); 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) { if (node_new == NULL) {
perror("Error (linked_list_add_after_last)"); perror("Error (linked_list_add_after_last)");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

View File

@ -1,7 +1,7 @@
#include "queue.h" #include "queue.h"
struct queue *queue_initialization() { struct queue *queue_initialization() {
struct queue *queue = malloc(sizeof(*queue)); struct queue *queue = malloc(sizeof(struct queue));
if (queue == NULL) { if (queue == NULL) {
perror("Error (queue_initialization)"); perror("Error (queue_initialization)");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -17,7 +17,7 @@ void queue_push(struct queue *queue, void *data) {
perror("Error (queue_push)"); perror("Error (queue_push)");
exit(EXIT_FAILURE); 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) { if (node_new == NULL) {
perror("Error (queue_push)"); perror("Error (queue_push)");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

View File

@ -1,7 +1,7 @@
#include "stack.h" #include "stack.h"
struct stack *stack_initialization() { struct stack *stack_initialization() {
struct stack *stack = malloc(sizeof(*stack)); struct stack *stack = malloc(sizeof(struct stack));
if (stack == NULL) { if (stack == NULL) {
perror("Error (stack_initialization)"); perror("Error (stack_initialization)");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -17,7 +17,7 @@ void stack_push(struct stack *stack, void *data) {
perror("Error (stack_push)"); perror("Error (stack_push)");
exit(EXIT_FAILURE); 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) { if (data == NULL) {
perror("Error (stack_push)"); perror("Error (stack_push)");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);