init函数可以多次调用,但是为了防止内存泄漏。因此每次初始化时先执行一次destory操作

This commit is contained in:
建峰 2024-08-29 18:30:48 +08:00
parent 43cfbfcda6
commit d8012720b6
4 changed files with 23 additions and 19 deletions

3
.vscode/launch.json vendored
View File

@ -8,7 +8,8 @@
"name": "unicstl", "name": "unicstl",
"type": "cppdbg", "type": "cppdbg",
"request": "launch", "request": "launch",
"program": "${workspaceFolder}/build/release/bin/test.exe", // "program": "${workspaceFolder}/build/release/bin/test.exe",
"program": "${workspaceFolder}/build/release/bin/demo.exe",
"args": [], "args": [],
"stopAtEntry": false, "stopAtEntry": false,
"cwd": "${fileDirname}", "cwd": "${fileDirname}",

View File

@ -58,10 +58,10 @@ int main()
printf("----- unicstl demo -----\n"); printf("----- unicstl demo -----\n");
// while (1) // while (1)
{ {
demo_list(); // demo_list();
// demo_stack(); // demo_stack();
// demo_deque(); // demo_deque();
// demo_queue(); demo_queue();
// demo_tree(); // demo_tree();
// demo_heap(); // demo_heap();
} }

View File

@ -134,12 +134,15 @@ static bool queue_clear(struct _queue* self)
static bool queue_empty(struct _queue* self) static bool queue_empty(struct _queue* self)
{ {
assert(self != NULL); assert(self != NULL);
return self->_size == 0; assert(self->size != NULL);
return self->size(self) == 0;
} }
static bool queue_full(struct _queue* self) static bool queue_full(struct _queue* self)
{ {
assert(self != NULL); assert(self != NULL);
assert(self->size != NULL);
assert(self->capacity != NULL);
return self->size(self) == self->capacity(self); return self->size(self) == self->capacity(self);
} }
@ -307,12 +310,6 @@ bool queue_init(struct _queue * queue, uint32_t obj_size)
return false; return false;
} }
// The queue is only initialized once
if(queue->_obj_size != 0)
{
return false;
}
// attribute init // attribute init
queue->_size = 0; queue->_size = 0;
queue->_obj_size = obj_size; queue->_obj_size = obj_size;
@ -330,13 +327,19 @@ bool queue_init(struct _queue * queue, uint32_t obj_size)
queue->empty = queue_empty; queue->empty = queue_empty;
queue->full = queue_full; queue->full = queue_full;
queue->size = queue_size; queue->size = queue_size;
queue->capacity = queue_capacity;
queue->destory = queue_destory; queue->destory = queue_destory;
queue->print = queue_print; queue->print = queue_print;
// 防止多次调用init导致内存泄漏
queue->destory(queue);
// init front & back // init front & back
queue->_front = NULL; queue->_front = NULL;
queue->_back = NULL; queue->_back = NULL;
return true;
} }
bool queue_init2(struct _queue * queue, uint32_t obj_size, uint32_t capacity) bool queue_init2(struct _queue * queue, uint32_t obj_size, uint32_t capacity)
@ -349,12 +352,6 @@ bool queue_init2(struct _queue * queue, uint32_t obj_size, uint32_t capacity)
return false; return false;
} }
// The queue is only initialized once
if(queue->_obj_size != 0)
{
return false;
}
// attribute init // attribute init
queue->_size = 0; queue->_size = 0;
queue->_obj_size = obj_size; queue->_obj_size = obj_size;
@ -377,6 +374,11 @@ bool queue_init2(struct _queue * queue, uint32_t obj_size, uint32_t capacity)
queue->destory = queue2_destory; queue->destory = queue2_destory;
queue->print = queue2_print; queue->print = queue2_print;
// 防止多次调用init导致内存泄漏
queue->_front = NULL;
queue->_back = NULL;
queue->destory(queue);
// init front & back // init front & back
queue->_front = (struct _queue_node *)malloc(sizeof(struct _queue_node)); queue->_front = (struct _queue_node *)malloc(sizeof(struct _queue_node));
if(queue->_front == NULL) if(queue->_front == NULL)

View File

@ -33,7 +33,7 @@ static void test_queue_init(void)
TEST_ASSERT_FALSE(queue_init(NULL, sizeof(int))); TEST_ASSERT_FALSE(queue_init(NULL, sizeof(int)));
TEST_ASSERT_FALSE(queue_init(queue, 0)); TEST_ASSERT_FALSE(queue_init(queue, 0));
TEST_ASSERT_TRUE(queue_init(queue, sizeof(int))); TEST_ASSERT_TRUE(queue_init(queue, sizeof(int)));
TEST_ASSERT_FALSE(queue_init(queue, sizeof(int))); // The queue is only initialized once TEST_ASSERT_TRUE(queue_init(queue, sizeof(char)));
queue_free(&queue); queue_free(&queue);
// ------------------------------ // ------------------------------
@ -42,7 +42,7 @@ static void test_queue_init(void)
TEST_ASSERT_FALSE(queue_init2(queue, 0, 1)); TEST_ASSERT_FALSE(queue_init2(queue, 0, 1));
TEST_ASSERT_FALSE(queue_init2(queue, sizeof(int), 0)); TEST_ASSERT_FALSE(queue_init2(queue, sizeof(int), 0));
TEST_ASSERT_TRUE(queue_init2(queue, sizeof(int), 1)); TEST_ASSERT_TRUE(queue_init2(queue, sizeof(int), 1));
TEST_ASSERT_FALSE(queue_init2(queue, sizeof(int), 1)); // The queue is only initialized once TEST_ASSERT_TRUE(queue_init2(queue, sizeof(int), 5));
queue_free(&queue); queue_free(&queue);
} }
@ -56,7 +56,8 @@ static void test_queue_push(void)
queue_t queue = NULL; queue_t queue = NULL;
// ------------------------------ queue = queue_new(); // ------------------------------
queue = queue_new();
queue_init(queue, sizeof(int)); queue_init(queue, sizeof(int));
TEST_ASSERT_TRUE(queue->empty(queue)); TEST_ASSERT_TRUE(queue->empty(queue));
for(i = 0; i < len; i++) for(i = 0; i < len; i++)