From 11393048bbe014e518309af5619eabb104b3a619 Mon Sep 17 00:00:00 2001 From: Kapliuk Date: Thu, 14 Mar 2024 21:36:06 +0000 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=BD=D0=BE=D0=B2=D0=B8=D1=82=D0=B8=20'c?= =?UTF-8?q?v4/list=5Fops.c'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cv4/list_ops.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/cv4/list_ops.c b/cv4/list_ops.c index 7343a68..e451fd8 100644 --- a/cv4/list_ops.c +++ b/cv4/list_ops.c @@ -5,17 +5,16 @@ #include list_t *new_list(size_t length, list_element_t elements[]) { - list_t *list = malloc(sizeof(list_t)); + list_t *list = malloc(sizeof(list_t) + length * sizeof(list_element_t)); if (!list) { - return NULL; - } - list->elements = malloc(length * sizeof(list_element_t)); - if (!list->elements) { - free(list); - return NULL; + return NULL; } + list->length = length; - memcpy(list->elements, elements, length * sizeof(list_element_t)); + for (size_t i = 0; i < length; i++) { + list->elements[i] = elements[i]; + } + return list; }