diff options
author | Paul Duncan <pabs@pablotron.org> | 2019-02-03 06:06:13 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2019-02-03 06:06:13 -0500 |
commit | b4324a1d3c9f0152343c8a3f0480125deea5c48c (patch) | |
tree | da386f51b2a1d0b59fbba666f41829dea8c24163 /km-set.c | |
parent | f741aac2d3319900443d9f38ff4cff48b0484d7a (diff) | |
download | kmeans-b4324a1d3c9f0152343c8a3f0480125deea5c48c.tar.bz2 kmeans-b4324a1d3c9f0152343c8a3f0480125deea5c48c.zip |
fix km_set_grow()
Diffstat (limited to 'km-set.c')
-rw-r--r-- | km-set.c | 33 |
1 files changed, 21 insertions, 12 deletions
@@ -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 |