簡體   English   中英

C鏈表valgrind讀取的大小無效

[英]C Linked List valgrind Invalid Read of Size

我的鏈表和valgrind輸出有問題。 沒有更多的理由,這是我的鏈表:

typedef struct Map map;

struct Map
{
    void *address;
    double free_time;
    map* next;
}*map_list;

該列表是使用虛擬頭節點創建的。 如您所見,該結構包含一個地址和一個空閑時間,我試圖將它們關聯起來。

find_and_free函數中,我使用一個時間搜索此列表,如果此時間小於列表中存儲的時間,則我將取消分配已保存的地址。 然后我也取消分配列表節點。

這是用於查找小於我所經過的空閑時間的函數。 如果較小,則釋放存儲到列表中的地址,然后調用delete_map_node函數以重新分配列表的節點。

void find_and_free_address(map *root, double mtime)
{
    map *current = root->next;
    assert(current);
    while(current)
    {
        if(current->free_time < mtime)
        {

            printf("there is something to FREE now\n");
            printf("the time to check for free is %lf and the maps free time is %lf\n", mtime,current->free_time);
            printf("The map contains an address that is time to free\n");
            //free_allocated_address(&current->address);
            free(current->address);
            delete_map_node(map_list, current->free_time);
            //delete(map_list,current->free_time);
            //return next;
        }

        else
        {
            printf("there is nothing to free now\n");
        }

        current = current->next; //FIRST ERROR
    }
    printf("THE MAP SIZE AFTER REMOVALS IS %d\n", map_size(map_list));
}

這是delete_map_node函數

map* delete_map_node(map *root,double ftime)
{
    if (root==NULL)
    {
        return NULL;
    }

    //map *temporary;

    if (root->free_time == ftime)
    {
        map *temporary = root->next;
        free(root); //SECOND ERROR
        root = temporary;
        return temporary;
    }

    root->next = delete_map_node(root->next, ftime);
    //free(root->address);
    return root;
}

我知道這兩個功能只能組合為一個功能。

valgrind,報告沒有內存泄漏或未初始化的值。 但是,當我執行以下命令時:

valgrind --tool=memcheck --leak-check=full --track-origins=yes -v ./a.out

我得到以下輸出:

==6807== Invalid read of size 4
==6807==    at 0x8049228: find_and_free_address (Map.c:123)
==6807==    by 0x8048DA6: second_iteration (List.c:150)
==6807==    by 0x8048C6B: first_iteration (List.c:113)
==6807==    by 0x8048908: main (Fscanf.c:63)
==6807==  Address 0x42005bc is 12 bytes inside a block of size 16 free'd
==6807==    at 0x402AF3D: free (vg_replace_malloc.c:468)
==6807==    by 0x804929F: delete_map_node (Map.c:142)
==6807==    by 0x80492C1: delete_map_node (Map.c:147)
==6807==    by 0x8049216: find_and_free_address (Map.c:113)
==6807==    by 0x8048DA6: second_iteration (List.c:150)
==6807==    by 0x8048C6B: first_iteration (List.c:113)
==6807==    by 0x8048908: main (Fscanf.c:63)

我可以看到錯誤是釋放它們后,我訪問root->nextcurrent->next ,但是沒有它我還是無法做到。

您能建議我一種擺脫該錯誤的方法嗎?

我看到的一個問題是,在delete_map_node您釋放了root (可能是從map_list傳遞的find_and_free_address ),但實際上並沒有更改map_list ,這意味着,當delete_map_node返回map_list變量時,它指向未分配的內存。 之后訪問map_list會導致未定義的行為

一個簡單的解決方案是將delete_map_node的返回值分配給map_list

map_list = delete_map_node(map_list, current->free_time);

另外,當delete_map_node釋放find_and_free_address函數中current列表中的節點時,會發生什么情況? 那么current = current->next也將導致不確定的行為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM