From 560301dbfe12c90e08f318b7a06c7f087f50cafa Mon Sep 17 00:00:00 2001 From: Joe Conigliaro Date: Wed, 26 May 2021 14:51:20 +1000 Subject: [PATCH] cgen: dont preallocate zero length with capacity of 1 --- vlib/builtin/array.v | 8 +++----- vlib/v/gen/c/cgen.v | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index ec006613e4..0636598f2b 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -95,13 +95,11 @@ fn (mut a array) ensure_cap(required int) { cap *= 2 } new_size := cap * a.element_size - mut new_data := &byte(0) - if a.data != voidptr(0) { - new_data = unsafe { realloc_data(a.data, a.cap * a.element_size, new_size) } + a.data = if a.data != voidptr(0) { + unsafe { realloc_data(a.data, a.cap * a.element_size, new_size) } } else { - new_data = vcalloc(new_size) + vcalloc(new_size) } - a.data = new_data a.cap = cap } diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 2ff38ef567..b12d0c3150 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5978,7 +5978,7 @@ fn (mut g Gen) type_default(typ_ ast.Type) string { elem_type_str = elem_type_str[3..] } noscan := g.check_noscan(elem_typ) - init_str := '__new_array${noscan}(0, 1, sizeof($elem_type_str))' + init_str := '__new_array${noscan}(0, 0, sizeof($elem_type_str))' if typ.has_flag(.shared_f) { atyp := '__shared__Array_${g.table.get_type_symbol(elem_typ).cname}' return '($atyp*)__dup_shared_array(&($atyp){.mtx = {0}, .val =$init_str}, sizeof($atyp))'