C Linked List valgrind Invalid Read of Size

前端 未结 1 1361
不知归路
不知归路 2021-01-25 04:04

I have a problem with my Linked List and the valgrind output. Without further adieu here is my linked list:

typedef struct Map map;

struct Map
{
    void *addr         


        
相关标签:
1条回答
  • 2021-01-25 05:03

    One problem that I see is that in delete_map_node you free root (which might be map_list passed from find_and_free_address), but you don't actually change map_list which means that when delete_map_node returns the map_list variable points to unallocated memory. Accessing map_list afterwards leads to undefined behavior.

    The simple solution to this is to assign the return value of delete_map_node to map_list:

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

    Also, what happens when delete_map_node frees the node in the list that is current in the find_and_free_address function? Then current = current->next will also lead to undefined behavior.

    0 讨论(0)
提交回复
热议问题