删除头功能中的内存泄漏

时间:2013-10-02 23:42:22

标签: c memory-leaks linked-list

嘿大家我在删除头功能中遇到内存泄漏。我想可能是因为我没有释放头部,但我似乎无法找到正确的位置。我正在思考L-> head = L-> head-> next;

data_t * removehead(list_t *L) {
  data_t *temp = NULL;
}
if (L->head != NULL) {
  temp = L->head->data_ptr;
  L->head = L->head->next;
  L->size--;
}
return temp;
}

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

这是一个品味问题,如果你不想声明一个特定的变量,那么你就不得不在L->head = ..之前做到这一点,否则你将不再有任何对旧头的引用。

但你总能以更易读的方式:

element *old_head = L->head;
temp = L->head->data_ptr;
L->head = L->head->next;
--L->size;
free(old_head);

我唯一想知道的是老头的尖头data_t*会发生什么?因为如果你没有任何参考,它也会泄漏。

答案 1 :(得分:1)

你可以有两个功能,一个用于显示当前列表头,另一个用于删除/弹出当前列表头,

data_t*
listHead(list_t* lp)
{
    if(!lp) return lp;
    if(!lp->head) return NULL;
    data_t* dp = lp->head->data;
    return dp;
}

data_t*
listPop(list_t* lp)
{
    if(!lp) return NULL;
    data_t* dp = NULL;
    if(lp->head)
    {
    if(lp->head == lp->tail) lp->tail = NULL;
    node_t* head = lp->head;
    lp->head = lp->head->next;
    dp = head->data;
    nodeDel(head);
    lp->size--;
    }
    return dp;
}

一个完整的(单个)链表; - ),

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef void data_t;
data_t*
dataNew(data_t* dp,int size)
{
    if(!dp) return NULL;
    data_t* data = (data_t*)malloc(size);
    if(!data) return data;
    memcpy(data,dp,size);
    return(data);
}
void
dataDel(data_t* dp)
{
    if(!dp) return;
    free(dp);
    return;
}

typedef struct
{
    void* next;
    data_t* data;
} node_t;
node_t*
nodeNew(data_t* data)
{
    node_t* np = (node_t*)malloc(sizeof(node_t));
    if(!np) return np;
    np->next=NULL;
    np->data = data;
    return(np);
}
data_t*
nodeDel(node_t* np)
{
    if(!np) return np;
    data_t* data = np->data;
    free(np);
    return(data);
}

typedef struct
{
    int size;
    node_t* head;
    node_t* tail;
} list_t;

list_t*
listNew()
{
    list_t* lp = (list_t*)malloc(sizeof(list_t));
    if(!lp) return lp;
    lp->head = lp->tail = NULL;
    lp->size=0;
    return(lp);
}
int
listEmpty(list_t* lp)
{
    int count=0;
    if(!lp) return 0;
    while (lp->head)
    {
        node_t* np;
        data_t* data;
        count++;
        np = lp->head;
        lp->head = lp->head->next;
        data = np->data;
        free(np);
        free(data);
    }
    return count;
}
int
listDel(list_t* lp)
{
    if(!lp) return 0;
    int count=listEmpty(lp);
    free(lp);
    return count;
}
int
listCount(list_t* lp)
{
    if(!lp) return 0;
    return(lp->size);
}
data_t*
listHead(list_t* lp)
{
    if(!lp) return lp;
    if(!lp->head) return NULL;
    data_t* dp = lp->head->data;
    return dp;
}
data_t*
listTail(list_t* lp)
{
    if(!lp) return lp;
    if(!lp->tail) return NULL;
    data_t* dp = lp->tail->data;
    return dp;
}
node_t*
listPush(list_t* lp, data_t* data)
{
    if(!lp) return NULL;
    if(!data) return NULL;
    node_t* np = nodeNew(data);
    if(!np) return np;
    if(lp->tail)
    {
    lp->tail = lp->tail->next = np;
    }
    if(!lp->head)
    {
    lp->head = lp->tail = np;
    }
    lp->size++;
    return np;
}
data_t*
listPop(list_t* lp)
{
    if(!lp) return NULL;
    data_t* dp = NULL;
    if(lp->head)
    {
    if(lp->head == lp->tail) lp->tail = NULL;
    node_t* head = lp->head;
    lp->head = lp->head->next;
    dp = head->data;
    nodeDel(head);
    lp->size--;
    }
    return dp;
}

int
main()
{
    list_t* lp;
    data_t* data;
    int ndx;
    if( !(lp = listNew()) )
    {
    printf("error: cannot allocate list\n"); exit(1);
    }
    for( ndx=0; ndx<100; ++ndx )
    {
        data = (data_t*)malloc(100);
        sprintf(data,"node:%d",ndx);
        if( !listPush(lp,data) )
        {
        printf("error: cannot push %s\n",data);
        free(data);
        }
    }

    while( listCount(lp) > 0 )
    {
        data = listPop(lp);
        printf("data: %s\n",data);
        free(data);
    }
}