aboutsummaryrefslogtreecommitdiff
path: root/collections/double_ended_queue/double_ended_queue.c
diff options
context:
space:
mode:
authorTucker Evans <tucker@tuckerevans.com>2020-07-06 11:13:24 -0400
committerTucker Evans <tucker@tuckerevans.com>2020-07-06 11:18:37 -0400
commitf1b0595ef2549c18b995f182a8ed8e6e44b9cd04 (patch)
treecd7a8f6eccf879f2f5b1dc3b5af544525fb8b500 /collections/double_ended_queue/double_ended_queue.c
parente1f7e38d660e46b1e32455595c4829f294a2740b (diff)
Fix double ended queue did not print free strings
Because it is possible for to_string function to generate a new string here and nothing else sees it after printing it is not guaranteed to be freed leading to a memory leak. This does mean that if the string is internal to a struct and expected to be used later then to_string should duplicate said string (as noted in the documentation).
Diffstat (limited to 'collections/double_ended_queue/double_ended_queue.c')
-rw-r--r--collections/double_ended_queue/double_ended_queue.c5
1 files changed, 4 insertions, 1 deletions
diff --git a/collections/double_ended_queue/double_ended_queue.c b/collections/double_ended_queue/double_ended_queue.c
index 28ed9dc..c08169d 100644
--- a/collections/double_ended_queue/double_ended_queue.c
+++ b/collections/double_ended_queue/double_ended_queue.c
@@ -147,12 +147,15 @@ deq *root;
char* to_string(void*);
{
int i, size;;
+ char *tmp;
size = deq_size(root);
printf("[");
for (i = 0; i < size; i++) {
- printf("%s,", to_string(deq_index(root, i)));
+ printf("%s,", tmp = to_string(deq_index(root, i)));
+ free(tmp);
+ tmp = NULL;
}
printf("\b]\n");
}