Automatically free container elements if needed

This commit is contained in:
2024-10-26 09:45:22 +11:00
parent 3148a56ce0
commit 2ee19c7c66
4 changed files with 21 additions and 7 deletions

View File

@@ -5,13 +5,18 @@
#include <stdlib.h>
Toy_Array* Toy_resizeArray(Toy_Array* paramArray, unsigned int capacity) {
//TODO: slip in a call to free the complex values here
if (capacity == 0) {
free(paramArray);
return NULL;
}
//if some values will be removed, free them first
if (paramArray != NULL && paramArray->count > capacity) {
for (unsigned int i = capacity; i < paramArray->count; i++) {
Toy_freeValue(paramArray->data[i]);
}
}
unsigned int originalCapacity = paramArray == NULL ? 0 : paramArray->capacity;
Toy_Array* array = realloc(paramArray, capacity * sizeof(Toy_Value) + sizeof(Toy_Array));