简体   繁体   English

Removehead功能中的内存泄漏

[英]Memory leak in removehead function

Hey everyone I am getting a memory leak in my remove head function. 嗨,大家好,我的移除磁头功能出现内存泄漏。 I think it could be that im not freeing the head but I can't seem to find the correct place to put it. 我认为可能是即时通讯没有释放头部,但我似乎找不到正确的放置位置。 I was thinking right above the line that says L->head = L->head->next; 我在说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;
}

Any thoughts? 有什么想法吗?

It's a matter of taste, if you don't want to declare a specific variable then you are forced to do it before L->head = .. or you won't have any reference to old head anymore. 这是一个问题,如果您不想声明特定的变量,则必须在L->head = ..之前执行此操作,否则将不再引用旧的head。

But you can always have a more readable way: 但是,您始终可以采用一种更具可读性的方式:

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

The only thing I'm wondering about is what happens to pointed data_t* of old head? 我唯一想知道的是data_t*指向data_t*会发生什么? Because it will leak too if you don't have any reference to it around. 因为如果您周围没有任何引用,它也会泄漏。

You could have two functions, one that shows the current list head, and another that drops/pops the current list head, 您可能有两个功能,一个显示当前列表的标题,另一个显示/弹出当前列表的标题,

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;
}

And a complete (singly) linked list ;-), 以及完整的(单个)链接列表;-),

#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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM