小编Abe*_*rte的帖子

在C中实现负责释放值的链表?

我正在用C实现一个链表,我遇到的问题是C没有实现任何特定的内存管理方案,只是让你能够通过传递指针来分配和释放内存.没有关于程序中是否可能需要该值的概念.

我在线查找链表的典型实现基本上解除了已删除节点的dealloc,但没有取消分配节点的值.

当从列表中删除时,释放由值占用的内存应该是谁的责任?链表或程序的正常流程?

例:

// allocate 10 bytes
char *text = malloc(sizeof(char) * 10);

// create the linked list
LinkedList *list = list_create();

// add the text pointer to the linked list
list_append(list, text);
// remove the pointer from the linked list
list_remove_last(list);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,文本最终不会被释放,因为list_remove_last只释放新节点占用的内存.释放文本所占用的内存的正确方法是什么?

c memory-management linked-list

8
推荐指数
1
解决办法
532
查看次数

标签 统计

c ×1

linked-list ×1

memory-management ×1