From 3d62254eb8827f9ef7f2316f2ccec2ac523c1cd0 Mon Sep 17 00:00:00 2001 From: Michael Tryby Date: Wed, 10 Apr 2019 11:11:13 -0400 Subject: [PATCH] Update list.c Updating in line comments. --- src/util/list.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/util/list.c b/src/util/list.c index 8ebb23e..7fe5f36 100644 --- a/src/util/list.c +++ b/src/util/list.c @@ -113,8 +113,8 @@ void *head_list(list_t *list, bool removeFromList) assert(list->head != NULL); listNode *node = list->head; + // Allocating and copying pointer to node data void *element = (void *)malloc(list->elementSize); - // Copying pointer to node->data memcpy(element, node->data, list->elementSize); if(removeFromList) { @@ -126,18 +126,25 @@ void *head_list(list_t *list, bool removeFromList) free(node->data); free(node); } - // Now element points to data formerly pointed to by node + // Now element points to data formerly pointed to by node. Caller + // is responsible for freeing both pointer to data and data. return element; } void *tail_list(list_t *list) +// +// Warning: Caller is responsible for freeing the node->data returned. +// { assert(list->tail != NULL); listNode *node = list->tail; + // Allocating and copying pointer to node data void *element = (void *)malloc(list->elementSize); memcpy(element, node->data, list->elementSize); + // Pointer to element data gets returned. Caller is responsible + // for freeing pointer to data. return element; }