From c1d43cddee94dd370078f755d33147c9a8181852 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 30 Oct 2024 16:46:13 +0100 Subject: use C++-style memory management in List container --- backend/List.hpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'backend') diff --git a/backend/List.hpp b/backend/List.hpp index a0d9289..17b6ce7 100644 --- a/backend/List.hpp +++ b/backend/List.hpp @@ -9,10 +9,11 @@ size_t List::size() { template void List::push_back(T & el) { - ListLink * link = static_cast *>(malloc(sizeof(ListLink))); - link->prev = this->head; - link->next = nullptr; - link->value = el; + ListLink * link = new ListLink { + .prev = this->head, + .next = nullptr, + .value = el, + }; if (this->head != nullptr) this->head->next = link; if (this->head == nullptr) this->tail = link; this->head = link; @@ -29,7 +30,7 @@ void List::remove(const T & target) { if (link->next != nullptr) link->next->prev = link->prev; if (link->prev != nullptr) link->prev->next = link->next; - free(link); + delete link; this->length--; } @@ -40,7 +41,7 @@ void List::pop_back() { ListLink * secondlast = this->head->prev; if (secondlast != nullptr) secondlast->next = nullptr; - free(this->head); + delete this->head; if (this->tail == this->head) this->tail = nullptr; this->head = secondlast; @@ -53,7 +54,7 @@ void List::clear() { ListLink * link = this->tail; while (link != nullptr) { ListLink * next = link->next; - free(link); + delete link; link = next; } this->head = nullptr; -- cgit v1.2.3