1

大家好,我的删除头功能出现内存泄漏。我认为这可能是我没有释放头部,但我似乎无法找到正确的放置位置。我在想 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;
}

有什么想法吗?

4

2 回答 2

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*老头的尖头会发生什么?因为如果您周围没有任何参考,它也会泄漏。

于 2013-10-02T23:47:10.817 回答
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);
    }
}
于 2013-10-03T04:09:58.253 回答