--- libelwix/src/array.c 2019/01/21 11:56:39 1.5.36.1 +++ libelwix/src/array.c 2019/01/22 16:18:47 1.8 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: array.c,v 1.5.36.1 2019/01/21 11:56:39 misho Exp $ +* $Id: array.c,v 1.8 2019/01/22 16:18:47 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -74,6 +74,35 @@ array_Init(int numItems) } /* + * array_Init2() - Initialize dynamic array + * + * @arr = Allocated array variable + * @numItems = Number of Items + * return: NULL error, != NULL allocated memory for array + */ +array_t * +array_Init2(array_t * __restrict arr, int numItems) +{ + if (!arr) + return array_Init(numItems); + + if (array_Size(arr)) + return NULL; /* already allocated array! */ + + arr->arr_last = -1; + arr->arr_num = numItems; + arr->arr_data = e_calloc(array_Size(arr), sizeof(intptr_t)); + if (!arr->arr_data) { + e_free(arr); + return NULL; + } else + array_Zero(arr); + + return arr; +} + + +/* * array_From() - Create and fill array from array with pointers * * @pargv = Array with pointers @@ -185,15 +214,14 @@ array_Reset(array_t * __restrict arr, int purge) if (!arr) return; - arr->arr_last = -1; - arr->arr_num = 0; - if (purge && arr->arr_data) { e_free(arr->arr_data); + arr->arr_num = 0; arr->arr_data = e_calloc(array_Size(arr), sizeof(intptr_t)); } array_Zero(arr); + arr->arr_last = -1; } /*