diff options
author | Kimplul <kimi.h.kuparinen@gmail.com> | 2025-05-01 13:49:07 +0300 |
---|---|---|
committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2025-05-01 13:49:07 +0300 |
commit | 5a4cb1b5a8ba258a23a62feab1e66cc7d0eba3bf (patch) | |
tree | 7e0d400af6dc33ab013d602048da7a0f1e60d6b6 | |
parent | be71a36fd88941a5bc3f25d2e563c8aa0481327c (diff) | |
download | conts-5a4cb1b5a8ba258a23a62feab1e66cc7d0eba3bf.tar.gz conts-5a4cb1b5a8ba258a23a62feab1e66cc7d0eba3bf.zip |
fix zero-sized reservations
-rw-r--r-- | include/conts/vec.h | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/include/conts/vec.h b/include/conts/vec.h index 8da77cf..19fd18f 100644 --- a/include/conts/vec.h +++ b/include/conts/vec.h @@ -24,6 +24,9 @@ struct VEC_STRUCT { static inline struct VEC_STRUCT VEC(create)(size_t reserve) { + if (reserve == 0) + return (struct VEC_STRUCT) {.n = 0, .s = 0, .buf = NULL}; + return (struct VEC_STRUCT) { .n = 0, .s = reserve, @@ -96,7 +99,7 @@ static inline void VEC(reserve)(struct VEC_STRUCT *v, size_t n) return; while (v->s < v->n) - v->s *= 2; + v->s = v->s == 0 ? 1 : 2 * v->s; v->buf = realloc(v->buf, v->s * sizeof(VEC_TYPE)); } |