繁体   English   中英

哈希表valgrind内存泄漏

[英]Hashtable valgrind memory leak

嗨,我一直在从事学校项目,需要您提供代码帮助。 我使用valgrind找出错误所在,并且需要摆脱严重的错误,这些错误正是您的想法所意味着的

函数将新元素插入表

这是我得到的错误之一

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

您有一些分配错误,可能会导致未定义的行为。 我将从上至下深入研究代码

首先,您要分配大量内存

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

以下内容就足够了,因为nextitem-> token.data之后分配。

nextitem = malloc(sizeof(HTab_listitem));

另外,在分配token.data时,请使用以下命令:

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

您的第二项分配也不是正确的大小。 您分配一个指针的sizeof(8个字节)而不是您的结构的大小并添加令牌。数据仍然没有用。

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

那应该是:

 nextitem = malloc(sizeof(HTab_listitem));

然后像以前一样再次分配token.data。

暂无
暂无

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

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