From 489f68072f95b68722b73cc53ceb5b839d997e42 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Tue, 29 Mar 2016 00:23:49 +1300 Subject: Copy the arg strings rather than sharing them Avoiding strdup(3) as I'm still playing the C90-only game --- btree-str.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/btree-str.c b/btree-str.c index 1e0903a..ff2338e 100644 --- a/btree-str.c +++ b/btree-str.c @@ -42,6 +42,7 @@ void fn(Node *n) { fn(n->l); if (n->r) fn(n->r); + free(n->v); free(n); return; } @@ -50,8 +51,13 @@ int main(int argc, char **argv) { Node *r = NULL; for (argv++, argc--; argc; argv++, argc--) { + unsigned long l = 0; Node *n = malloc(sizeof(Node)); - n->v = *argv; + + l = strlen(*argv) + 1; + n->v = malloc(l); + strncpy(n->v, *argv, l); + n->l = n->r = NULL; an(&r, n); } -- cgit v1.2.3