[英]C Linked List valgrind Invalid Read of Size
I have a problem with my Linked List and the valgrind output. 我的链表和valgrind输出有问题。 Without further adieu here is my linked list:
没有更多的理由,这是我的链表:
typedef struct Map map;
struct Map
{
void *address;
double free_time;
map* next;
}*map_list;
The list is created using a dummy head node. 该列表是使用虚拟头节点创建的。 As you can see, the struct holds an address and a free time, which I try to associate them.
如您所见,该结构包含一个地址和一个空闲时间,我试图将它们关联起来。
In the find_and_free
function I search this list using a time and if this time is smaller than the one stored in the list, I deallocate the saved address. 在
find_and_free
函数中,我使用一个时间搜索此列表,如果此时间小于列表中存储的时间,则我将取消分配已保存的地址。 And then I deallocate the list node as well. 然后我也取消分配列表节点。
This is the function used to find any free time that is smaller than the one I am passing. 这是用于查找小于我所经过的空闲时间的函数。 If it is smaller, I free the address stored to the list, and then call the
delete_map_node
function to also deallocate the node of the list. 如果较小,则释放存储到列表中的地址,然后调用
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(¤t->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));
}
And this is the delete_map_node
function 这是
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;
}
I am aware that those two can be combined to only one function. 我知道这两个功能只能组合为一个功能。
valgrind, reports no memory leaks or uninitialized values. valgrind,报告没有内存泄漏或未初始化的值。 However when I execute the following command:
但是,当我执行以下命令时:
valgrind --tool=memcheck --leak-check=full --track-origins=yes -v ./a.out
I get the following output : 我得到以下输出:
==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)
I can see that the error is that I access root->next
and current->next
after I have freed them, but I have not managed to do without it. 我可以看到错误是释放它们后,我访问
root->next
和current->next
,但是没有它我还是无法做到。
Can you suggest me a way, to get rid of this error? 您能建议我一种摆脱该错误的方法吗?
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. 我看到的一个问题是,在
delete_map_node
您释放了root
(可能是从map_list
传递的find_and_free_address
),但实际上并没有更改map_list
,这意味着,当delete_map_node
返回map_list
变量时,它指向未分配的内存。 Accessing map_list
afterwards leads to undefined behavior . 之后访问
map_list
会导致未定义的行为 。
The simple solution to this is to assign the return value of delete_map_node
to map_list
: 一个简单的解决方案是将
delete_map_node
的返回值分配给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? 另外,当
delete_map_node
释放find_and_free_address
函数中current
列表中的节点时,会发生什么情况? Then current = current->next
will also lead to undefined behavior. 那么
current = current->next
也将导致不确定的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.