diff options
author | Tucker Evans <tucker@tuckerevans.com> | 2020-07-04 22:07:23 -0400 |
---|---|---|
committer | Tucker Evans <tucker@tuckerevans.com> | 2020-07-04 23:14:25 -0400 |
commit | 8aa9f42d914a453a0570752ee292b9a9cfcae9d6 (patch) | |
tree | d69dfda702898a808c89f432ccf7a46e5e2e8c7f | |
parent | 8874cf97227139ed10e75fe13108988b45492172 (diff) |
Fix vector print function now frees string representations
-rw-r--r-- | collections/vector/vector.adoc | 7 | ||||
-rw-r--r-- | collections/vector/vector.c | 5 |
2 files changed, 9 insertions, 3 deletions
diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index e3fa94a..ce91c71 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@ Vector ====== Tucker Evans -v0.15, 2020-07-04 +v0.16, 2020-07-04 A basic vector, that hold pointers to your data structures. @@ -112,6 +112,9 @@ brackets and separated by commas ','). +to_string+ is a function that takes a pointer to the type of elements stored in +self+ and returns a string representation. +NOTE: Strings are freed after printing, therefore +strdup()+ should be used on +any strings that are not for the sole purpose of printing here. + Examples ^^^^^^^^ [source,c] @@ -122,7 +125,7 @@ Examples char* to_string(str) void *str; { - return str; + return strdup(str); } int main() diff --git a/collections/vector/vector.c b/collections/vector/vector.c index c456d8a..391b5a6 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -108,10 +108,13 @@ vec *root; char* to_string(void*); { int i; + char *tmp; printf("["); for(i = 0; i < root->end; i++) { - printf("%s", to_string(vec_index(root, i))); + printf("%s", tmp = to_string(vec_index(root, i))); + free(tmp); + tmp = NULL; } printf("\b]\n"); |