C 链表剪切导致内存泄漏
我现在已经为这个问题挣扎了几个小时,在网上搜索了很多,但找不到解决办法 我创建了一个链表结构,其中有一个结构节点作为头。 现在,我想在一个特定的节点上拆分我的链表,返回第二个列表 代码:C 链表剪切导致内存泄漏,c,linked-list,nodes,C,Linked List,Nodes,我现在已经为这个问题挣扎了几个小时,在网上搜索了很多,但找不到解决办法 我创建了一个链表结构,其中有一个结构节点作为头。 现在,我想在一个特定的节点上拆分我的链表,返回第二个列表 代码: struct list* list_cut_after(struct list* l, struct node* n) { if (l && n) { if (n->next == NULL){ struct list *
struct list* list_cut_after(struct list* l, struct node* n) {
if (l && n) {
if (n->next == NULL){
struct list *l2 = list_init();
return l2;
}
struct list *l2 = list_init();
l2->head = n->next;
n->next = NULL;
}
else {
return NULL;
}
}
START_TEST (test_cut)
{
struct list* l = list_init();
struct node* n;
for (int i = 1; i < 6; i++) {
n = list_new_node(i);
list_add_back(l, n);
}
struct node *y = list_get_ith(l, 2);
list_cut_after(l, y);
list_cleanup(l);
}
END_TEST
int list_unlink_node(struct list* l, struct node* n) {
if (n != NULL || l != NULL) {
if (n == l->head){
l->head = list_next(n);
n->next = NULL;
return 0;
}
else {
list_prev(l, n)->next = list_next(n);
n->next = NULL;
return 0;
}
}
else {
return 1;
}
}
void list_free_node(struct node* n) {
free(n);
}
int list_cleanup(struct list* l) {
if (l){
struct node *current = list_head(l);
struct node *next;
while (current != NULL)
{
next = list_next(current);
list_unlink_node(l, current);
list_free_node(current);
current = next;
}
free(l);
return 0;
}
else {
return 1;
}
}
这段代码似乎对我有用,它使给定节点旁边的节点成为列表2的头部。但这不适合我,我在测试时有记忆障碍:
测试代码(所有其他功能正常工作):
struct list* list_cut_after(struct list* l, struct node* n) {
if (l && n) {
if (n->next == NULL){
struct list *l2 = list_init();
return l2;
}
struct list *l2 = list_init();
l2->head = n->next;
n->next = NULL;
}
else {
return NULL;
}
}
START_TEST (test_cut)
{
struct list* l = list_init();
struct node* n;
for (int i = 1; i < 6; i++) {
n = list_new_node(i);
list_add_back(l, n);
}
struct node *y = list_get_ith(l, 2);
list_cut_after(l, y);
list_cleanup(l);
}
END_TEST
int list_unlink_node(struct list* l, struct node* n) {
if (n != NULL || l != NULL) {
if (n == l->head){
l->head = list_next(n);
n->next = NULL;
return 0;
}
else {
list_prev(l, n)->next = list_next(n);
n->next = NULL;
return 0;
}
}
else {
return 1;
}
}
void list_free_node(struct node* n) {
free(n);
}
int list_cleanup(struct list* l) {
if (l){
struct node *current = list_head(l);
struct node *next;
while (current != NULL)
{
next = list_next(current);
list_unlink_node(l, current);
list_free_node(current);
current = next;
}
free(l);
return 0;
}
else {
return 1;
}
}
清理代码:
struct list* list_cut_after(struct list* l, struct node* n) {
if (l && n) {
if (n->next == NULL){
struct list *l2 = list_init();
return l2;
}
struct list *l2 = list_init();
l2->head = n->next;
n->next = NULL;
}
else {
return NULL;
}
}
START_TEST (test_cut)
{
struct list* l = list_init();
struct node* n;
for (int i = 1; i < 6; i++) {
n = list_new_node(i);
list_add_back(l, n);
}
struct node *y = list_get_ith(l, 2);
list_cut_after(l, y);
list_cleanup(l);
}
END_TEST
int list_unlink_node(struct list* l, struct node* n) {
if (n != NULL || l != NULL) {
if (n == l->head){
l->head = list_next(n);
n->next = NULL;
return 0;
}
else {
list_prev(l, n)->next = list_next(n);
n->next = NULL;
return 0;
}
}
else {
return 1;
}
}
void list_free_node(struct node* n) {
free(n);
}
int list_cleanup(struct list* l) {
if (l){
struct node *current = list_head(l);
struct node *next;
while (current != NULL)
{
next = list_next(current);
list_unlink_node(l, current);
list_free_node(current);
current = next;
}
free(l);
return 0;
}
else {
return 1;
}
}
list\u cut\u after
返回指向新列表的指针,但不保存它。这意味着内存丢失,并且出现了泄漏
保存此函数的返回值,然后稍后清理返回的列表
struct list *l2 = list_cut_after(l, y);
list_cleanup(l);
list_cleanup(l2);
list\u cut\u after
返回指向新列表的指针,但不保存它。这意味着内存丢失,并且出现了泄漏
保存此函数的返回值,然后稍后清理返回的列表
struct list *l2 = list_cut_after(l, y);
list_cleanup(l);
list_cleanup(l2);