From 9d049ff861c336ab9098246d1dfa7471f6c16a03 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Sun, 5 Dec 2021 14:15:47 +0100 Subject: [PATCH] use uthash safely in day 5 --- 05a.c | 8 ++++++-- 05b.c | 7 +++++-- Makefile | 2 ++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/05a.c b/05a.c index cdebe38..8a2e9f1 100644 --- a/05a.c +++ b/05a.c @@ -1,4 +1,5 @@ #include +#include #include #define SWAP(x, y) { x ^= y; y ^= x; x ^= y; } @@ -9,13 +10,16 @@ struct entry { struct point key; int i; UT_hash_handle hh; }; void mark_point(int x, int y, struct entry **entries, int *r) { struct entry *p; - struct point s = { .x=x, .y=y }; + struct point s; + memset(&s, 0, sizeof s); + s.x = x; + s.y = y; HASH_FIND(hh, *entries, &s, sizeof(struct point), p); if (p) { if (p->i++ == 1) *r = *r+1; } else { - p = malloc(sizeof(struct entry)); + p = calloc(1, sizeof(struct entry)); p->key = s; p->i = 1; HASH_ADD(hh, *entries, key, sizeof(struct point), p); diff --git a/05b.c b/05b.c index f8dd141..509c555 100644 --- a/05b.c +++ b/05b.c @@ -9,13 +9,16 @@ struct entry { struct point key; int i; UT_hash_handle hh; }; void mark_point(int x, int y, struct entry **entries, int *r) { struct entry *p; - struct point s = {.x=x, .y=y}; + struct point s; + memset(&s, 0, sizeof s); + s.x = x; + s.y = y; HASH_FIND(hh, *entries, &s, sizeof(struct point), p); if (p) { if (p->i++ == 1) *r = *r+1; } else { - p = malloc(sizeof(struct entry)); + p = calloc(1, sizeof(struct entry)); p->key = s; p->i = 1; HASH_ADD(hh, *entries, key, sizeof(struct point), p); diff --git a/Makefile b/Makefile index 6749bac..dfdd641 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,8 @@ all: $(BINARIES) clean: $(RM) *.o a.out $(BINARIES) +05%: CFLAGS+=-DHASH_BLOOM + run: $(addprefix run_,$(BINARIES)) run_%a: %a -- 2.20.1