list_empty will now deallocate data completely

This commit is contained in:
Sara 2023-11-06 21:15:34 +01:00
parent 0d711f7ef4
commit 67b751c136

View file

@ -37,8 +37,8 @@ List list_copy(const List* source) {
void list_empty(List* self) {
if(self->data == NULL || self->cap == 0)
return;
self->data = realloc(self->data, LIST_DEFAULT_RESERVE * self->element_size);
self->cap = LIST_DEFAULT_RESERVE;
self->data = NULL;
self->cap = 0;
self->len = 0;
}
@ -51,7 +51,11 @@ void list_reserve(List* self, size_t at_least) {
new_cap *= 2;
}
void* new = realloc(self->data, new_cap * self->element_size);
void* new;
if(self->data == NULL)
new = malloc(new_cap * self->element_size);
else
new = realloc(self->data, new_cap * self->element_size);
ASSERT_RETURN(new != NULL,, "Failed to reserve space for %zu extra elements in list", new_cap);
self->data = new;