From 4d0f6767b1cbe8a0cf37a133275d330d02eb9b33 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 28 Oct 2021 17:18:03 +0300 Subject: [PATCH] builtin: use malloc instead of calloc for `[]Type{cap: size}` --- vlib/builtin/array.v | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index 6ef3d0a766..f447ab6ec5 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -34,10 +34,14 @@ fn __new_array_with_default(mylen int, cap int, elm_size int, val voidptr) array cap_ := if cap < mylen { mylen } else { cap } mut arr := array{ element_size: elm_size - data: vcalloc(cap_ * elm_size) len: mylen cap: cap_ } + if cap_ > 0 && mylen == 0 { + arr.data = unsafe { malloc(cap_ * elm_size) } + } else { + arr.data = vcalloc(cap_ * elm_size) + } if val != 0 { for i in 0 .. arr.len { unsafe { arr.set_unsafe(i, val) } @@ -50,7 +54,7 @@ fn __new_array_with_array_default(mylen int, cap int, elm_size int, val array) a cap_ := if cap < mylen { mylen } else { cap } mut arr := array{ element_size: elm_size - data: vcalloc(cap_ * elm_size) + data: unsafe { malloc(cap_ * elm_size) } len: mylen cap: cap_ } @@ -66,7 +70,7 @@ fn new_array_from_c_array(len int, cap int, elm_size int, c_array voidptr) array cap_ := if cap < len { len } else { cap } arr := array{ element_size: elm_size - data: vcalloc(cap_ * elm_size) + data: unsafe { malloc(cap_ * elm_size) } len: len cap: cap_ }