diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-30 16:46:13 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-30 16:46:13 +0100 |
commit | c1d43cddee94dd370078f755d33147c9a8181852 (patch) | |
tree | 70d0945bcbd5d11caffdb818b7357feae1ae8f73 /backend | |
parent | 504d231429e2a5d2b31a876645b590bb4a6a03f6 (diff) |
use C++-style memory management in List container
Diffstat (limited to 'backend')
-rw-r--r-- | backend/List.hpp | 15 |
1 files changed, 8 insertions, 7 deletions
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<T>::size() { template <typename T> void List<T>::push_back(T & el) { - ListLink<T> * link = static_cast<ListLink<T> *>(malloc(sizeof(ListLink<T>))); - link->prev = this->head; - link->next = nullptr; - link->value = el; + ListLink<T> * link = new ListLink<T> { + .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<T>::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<T>::pop_back() { ListLink<T> * 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<T>::clear() { ListLink<T> * link = this->tail; while (link != nullptr) { ListLink<T> * next = link->next; - free(link); + delete link; link = next; } this->head = nullptr; |