簡體   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