From d6aba8cb680498ca81668965a52e8b254db17f79 Mon Sep 17 00:00:00 2001 From: tmeissner Date: Sat, 16 May 2015 22:54:03 +0200 Subject: [PATCH] return value of realloc now written in tmp variable for proper handling of realloc errors --- 21st_century_c/chapter11/dict.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/21st_century_c/chapter11/dict.c b/21st_century_c/chapter11/dict.c index aa43c97..cd912d3 100644 --- a/21st_century_c/chapter11/dict.c +++ b/21st_century_c/chapter11/dict.c @@ -28,8 +28,10 @@ dictionary *dictionary_new(void) { static void dictionary_add_keyval(dictionary *in, keyval *kv) { in->length++; - in->pairs = realloc(in->pairs, sizeof(keyval*) * in->length); - if(in->pairs != NULL) { + keyval **tmp = realloc(in->pairs, sizeof(keyval*) * in->length); + if(tmp != NULL) { + in->pairs = tmp; + tmp = NULL; in->pairs[in->length-1] = kv; } else { fprintf(stderr, "Out of memory.\n");