From 479dc57ea8d1978cdb3047765fbac127355020e2 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Tue, 18 Jan 2022 16:28:49 +0000 Subject: [PATCH] Code cleanup in the List code. Possibly saved myself some headache in the future by using the right pointer type in the struct definition. --- includes/list.h | 2 +- src/list.c | 18 +++++++++--------- src/main.c | 15 ++------------- 3 files changed, 12 insertions(+), 23 deletions(-) diff --git a/includes/list.h b/includes/list.h index ea9fbd7..0fef199 100644 --- a/includes/list.h +++ b/includes/list.h @@ -9,7 +9,7 @@ #define LISTDEFAULTSIZE 4 typedef struct { - char** root; + void** content; int size; int capacity; } List; diff --git a/src/list.c b/src/list.c index 7268d79..3dcfdbe 100644 --- a/src/list.c +++ b/src/list.c @@ -8,9 +8,9 @@ List* CreateList() { return NULL; } - new->root = malloc(sizeof(void*) * LISTDEFAULTSIZE); + new->content = malloc(sizeof(void*) * LISTDEFAULTSIZE); - if (!new->root) { + if (!new->content) { fprintf(stderr, "Failed to malloc() memory for List contents.\n"); free(new); return NULL; @@ -28,27 +28,27 @@ int AddListItem(const void *value, size_t size, List* list) { if (size == 0) return - 1; if (list->capacity < list->size + 1) { - void* ptr = realloc(list->root, sizeof(void*) * list->capacity * 2); + void* ptr = realloc(list->content, sizeof(void*) * list->capacity * 2); //Note: realloc will free list->root if it succeeds. if (!ptr) { fprintf(stderr, "Failed to resize array with realloc() (%d bytes).\n", list->capacity * 2); return -1; } - list->root = ptr; + list->content = ptr; list->capacity = list->capacity * 2; } - void* item = malloc(size); + void* item = calloc(1, size); if (!item) { - fprintf(stderr, "Failed to malloc() new memory (%lu bytes).\n", size); + fprintf(stderr, "Failed to calloc() new memory (%lu bytes).\n", size); return -1; } memcpy(item, value, size); - list->root[list->size] = item; + list->content[list->size] = item; list->size++; return 0; @@ -58,9 +58,9 @@ void DestroyList(List* list) { if (!list) return; for(int i = 0; i < list->size; i++) { - free(list->root[i]); + free(list->content[i]); } - free(list->root); + free(list->content); free(list); } \ No newline at end of file diff --git a/src/main.c b/src/main.c index 25d9998..4558389 100644 --- a/src/main.c +++ b/src/main.c @@ -42,11 +42,12 @@ int main(int argc, char* args[]) { if (i % 4 == 0) printf("Size: %d; Capacity: %d\n", list->size, list->capacity); - printf("Some Value: %d Some Text: '%s'\n", ((struct test*) list->root[list->size - 1])->SomeValue, ((struct test*) list->root[list->size - 1])->SomeText); + printf("Some Value: %d Some Text: '%s'\n", ((struct test*) list->content[list->size - 1])->SomeValue, ((struct test*) list->content[list->size - 1])->SomeText); } //PrintList(list); free(thing->SomeText); + free(thing); free(list); } @@ -130,16 +131,4 @@ List* get_strings(const char* line) { } return list; -} - -void PrintList(const List* list) { - if (!list) return; - - const List *current = list; - - printf("Size: %d; Capacity: %d\n", list->size, list->capacity); - - for(int i = 0; i < list->size; i++) { - printf("%d: %s\n", i, list->root[i]); - } } \ No newline at end of file