From 2d3b6f1858c3e677f27587d4fdadf6fcdc774820 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Wed, 5 Jan 2022 16:02:06 +0000 Subject: [PATCH] Changed the signatures of some of the List functions to have the 'const' contract. I feel this makes more sense to have since these functions are to not modify all or some of their parameters. Plus it just feels right to have them defined this way. --- includes/list.h | 4 ++-- src/list.c | 14 ++++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/includes/list.h b/includes/list.h index dcff1f7..6d01119 100644 --- a/includes/list.h +++ b/includes/list.h @@ -14,8 +14,8 @@ typedef struct { } List; List* CreateList(void); -int AddListItem(char *, List *); -void PrintList(List*); +int AddListItem(const char *, List *); +void PrintList(const List*); void DestroyList(List*); #endif \ No newline at end of file diff --git a/src/list.c b/src/list.c index fa5fbbf..1fb806b 100644 --- a/src/list.c +++ b/src/list.c @@ -9,11 +9,17 @@ List* CreateList() { return new; } -int AddListItem(char *value, List* list) { +int AddListItem(const char *value, List* list) { if (list == NULL) return -1; if (list->capacity < list->size + 1) { - list->root = realloc(list->root, sizeof(char*) * list->capacity * 2); + void* ptr = realloc(list->root, sizeof(char*) * list->capacity * 2); + //Note: realloc will free list->root if it succeeds. + if (!ptr) { + return -1; + } + + list->root = ptr; list->capacity = list->capacity * 2; } @@ -28,10 +34,10 @@ int AddListItem(char *value, List* list) { return 0; } -void PrintList(List* list) { +void PrintList(const List* list) { if (list == NULL) return; - List *current = list; + const List *current = list; printf("Size: %d; Capacity: %d\n", list->size, list->capacity);