简体   繁体   English

哈希表valgrind内存泄漏

[英]Hashtable valgrind memory leak

Hi I've been working on school project and need your help with the code. 嗨,我一直在从事学校项目,需要您提供代码帮助。 I use valgrind to work out what is wrong and need to get rid of the awful errors what exacly means by your thoughts 我使用valgrind找出错误所在,并且需要摆脱严重的错误,这些错误正是您的想法所意味着的

Function inserts new element into table 函数将新元素插入表

This is one of the errors i get 这是我得到的错误之一

Invalid write of size 1 at 0x4C29B32: strcpy (vg_replace_strmem.c:458) by 0x401CE9: HTab_insert (ial.c:65) by 0x4019B5: main (main.c:82) Address 0x5449785 is 0 bytes after a block of size 5 alloc'd at 0x4C28FA4: malloc (vg_replace_malloc.c:296) by 0x401CC9: HTab_insert (ial.c:64) by 0x4019B5: main (main.c:82) 在0x4C29B32处大小为1的无效写入:strcpy(vg_replace_strmem.c:458)由0x401CE9:HTab_insert(ial.c:65)由0x4019B5:main(main.c:82)地址0x5449785是大小为5的块分配后的0字节d在0x4C28FA4:malloc(vg_replace_malloc.c:296)由0x401CC9:HTab_insert(ial.c:64)由0x4019B5:main(main.c:82)

HTab_listitem* HTab_insert(HTab_t* ptrht, Ttoken token) {
    unsigned ind = hash_function(ptrht->htable_size,token);
    HTab_listitem* item_ptr = NULL;
    HTab_listitem* item = ptrht->list[ind];
    HTab_listitem* nextitem;

    if(item == NULL) {
        nextitem = malloc(sizeof(HTab_listitem)+sizeof(char)*(strlen(token.data)+1));

        if(nextitem == NULL)
            /*allocation error*/
            return NULL;
        else {
            //printf("HERE\n");
            //printf("%s\n", token.data);
            //memcpy(nextitem->token.data,token.data,strlen(token.data)+1);
            int length = strlen(token.data);
            nextitem->token.data = malloc(length * sizeof((char) +2));
            strcpy(nextitem->token.data,token.data);
            nextitem->token.data[length] = '\0';
            nextitem->token.stav = token.stav;
            //printf("HERE AFTER\n");
            nextitem->ptrnext = NULL;

            item = ptrht->list[ind] = nextitem;

            nextitem = NULL;
            if(item == NULL)
                return NULL;
        }
    }
    else {
        while(item != NULL) {
            if(strcmp(item->token.data,token.data) == 0) {
                //if found
                item_ptr = item;
                break;
            }
            else {
                //next item
                item_ptr = item;
                item = item->ptrnext;
            }
        }
        if(item_ptr != NULL && item != item_ptr) {
            //not found insert next item
            nextitem = malloc(sizeof(HTab_listitem*)+sizeof(char)*(strlen(token.data)+1));
            if(nextitem == NULL)
                /*allocation error*/
                return NULL;
            else {
                //memcpy(nextitem->token.data,token.data,strlen(token.data)+1);
                int length = strlen(token.data);
                nextitem->token.data = malloc(length * sizeof((char) +2));
                strcpy(nextitem->token.data,token.data);
                nextitem->token.data[length] = '\0';
                nextitem->token.stav = token.stav;

                nextitem->ptrnext = NULL;
                item = nextitem;
                if(item == NULL)
                    return NULL;
                item_ptr->ptrnext = item;
            }
        }
    }
    return item;
}

You have a few allocation errors that could lead to undefined behavior. 您有一些分配错误,可能会导致未定义的行为。 I will go into the code from top to bottom 我将从上至下深入研究代码

First, you allocate to much memory 首先,您要分配大量内存

nextitem = malloc(sizeof(HTab_listitem)+sizeof(char)*(strlen(token.data)+1));

Where the following should be enough, since the nextitem->token.data is allocated afterwards. 以下内容就足够了,因为nextitem-> token.data之后分配。

nextitem = malloc(sizeof(HTab_listitem));

Also, when allocating token.data, use the following : 另外,在分配token.data时,请使用以下命令:

int length = strlen(token.data);
nextitem->token.data = malloc( sizeof(char) * (length + 1) );
nextitem->token.data[length] = 0;

Your second item allocation is again not the right size. 您的第二项分配也不是正确的大小。 You allocate the sizeof of a pointer (8 bytes) instead of the size of your struct and adding the token.data is still not usefull. 您分配一个指针的sizeof(8个字节)而不是您的结构的大小并添加令牌。数据仍然没有用。

nextitem = malloc(sizeof(HTab_listitem*)+sizeof(char)*(strlen(token.data)+1));
//Error here (HTab_listitem*)

That should be : 那应该是:

 nextitem = malloc(sizeof(HTab_listitem));

Then again allocate token.data as done previousely. 然后像以前一样再次分配token.data。

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

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