aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2019-02-03 06:06:13 -0500
committerPaul Duncan <pabs@pablotron.org>2019-02-03 06:06:13 -0500
commitb4324a1d3c9f0152343c8a3f0480125deea5c48c (patch)
treeda386f51b2a1d0b59fbba666f41829dea8c24163
parentf741aac2d3319900443d9f38ff4cff48b0484d7a (diff)
downloadkmeans-b4324a1d3c9f0152343c8a3f0480125deea5c48c.tar.bz2
kmeans-b4324a1d3c9f0152343c8a3f0480125deea5c48c.zip
fix km_set_grow()
-rw-r--r--km-set.c33
1 files changed, 21 insertions, 12 deletions
diff --git a/km-set.c b/km-set.c
index 63ec22a..58792c0 100644
--- a/km-set.c
+++ b/km-set.c
@@ -2,6 +2,7 @@
#include <stdint.h> // size_t
#include <string.h> // memcpy()
#include <stdlib.h> // rand()
+#include <errno.h> // errno
#include "util.h"
#include "km.h"
@@ -13,20 +14,28 @@ km_set_grow(
km_set_t * const set,
const size_t capacity
) {
- // alloc floats
- const size_t num_floats = set->shape.num_floats * capacity;
- float * const floats = malloc(sizeof(float) * num_floats);
- if (!floats) {
- // return failure
- return false;
+ float *floats = NULL;
+ const size_t floats_size = sizeof(float) * set->shape.num_floats * capacity;
+ // fprintf(stderr, "floats_size = %zu\n", floats_size);
+ if (floats_size > 0) {
+ // alloc floats
+ floats = realloc(set->floats, floats_size);
+ if (!floats) {
+ // return failure
+ return false;
+ }
}
- // alloc ints
- const size_t num_ints = set->shape.num_ints * capacity;
- int * const ints = malloc(sizeof(int) * num_ints);
- if (!ints) {
- // return failure
- return false;
+ int *ints = NULL;
+ const size_t ints_size = sizeof(int) * set->shape.num_ints * capacity;
+ // fprintf(stderr, "ints_size = %zu\n", ints_size);
+ if (ints_size > 0) {
+ // alloc ints
+ ints = realloc(set->ints, ints_size);
+ if (!ints) {
+ // return failure
+ return false;
+ }
}
// update set