From 6f9d7ef64f6dd33eed389e4d6cc4173ca6f2e7ad Mon Sep 17 00:00:00 2001 From: Gonglja Date: Wed, 18 Jan 2023 08:15:27 +0800 Subject: [PATCH] feat(codes/c): Fix the problem that mylist.c memory is not released --- codes/c/chapter_array_and_linkedlist/my_list.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/codes/c/chapter_array_and_linkedlist/my_list.c b/codes/c/chapter_array_and_linkedlist/my_list.c index 39a1aea58..879d74333 100644 --- a/codes/c/chapter_array_and_linkedlist/my_list.c +++ b/codes/c/chapter_array_and_linkedlist/my_list.c @@ -27,6 +27,12 @@ void newMyList(MyList *list) { list->extendRatio = 2; } +/* 析构函数 */ +void delMyList(MyList *list) { + list->size = 0; + free(list->nums); +} + /* 获取列表长度 */ int size(MyList *list) { return list->size; @@ -147,4 +153,7 @@ int main() { printf("扩容后的列表 list = "); printArray(toArray(&list), size(&list)); printf("容量 = %d ,长度 = %d\r\n", capacity(&list), size(&list)); + + /* 析构函数,释放分配内存 */ + delMyList(&list); } \ No newline at end of file