Free memory when reducing arrays

This commit is contained in:
Pierre Pronchery 2018-01-18 03:58:01 +01:00
parent ab2a660079
commit 91745593ce

View File

@ -152,12 +152,19 @@ int array_append(Array * array, void * value)
/* array_remove_pos */ /* array_remove_pos */
int array_remove_pos(Array * array, size_t pos) int array_remove_pos(Array * array, size_t pos)
{ {
char * p;
if(pos >= array->count) if(pos >= array->count)
return -error_set_code(-ERANGE, "%s", strerror(ERANGE)); return -error_set_code(-ERANGE, "%s", strerror(ERANGE));
array->count--; /* FIXME resize array? */ array->count--;
memmove(&array->value[pos * array->size], memmove(&array->value[pos * array->size],
&array->value[(pos + 1) * array->size], &array->value[(pos + 1) * array->size],
(array->count - pos) * array->size); (array->count - pos) * array->size);
if((p = realloc(array->value, array->size * array->count)) == NULL
&& array->count != 0)
/* XXX ignore the error */
return 0;
array->value = p;
return 0; return 0;
} }