如何释放calloc'd指针而不从哈希表/链表中删除值?

时间:2022-03-12 07:17:54

I'm setting up and loading a hashtable, using linked lists. For each item, I calloc a struct pointer, then enter that item into the array itself, or the associated linked list if the array index is taken.

我正在使用链接列表设置和加载哈希表。对于每个项目,我calloc一个struct指针,然后将该项输入到数组本身,或者如果采用数组索引则输入相关的链接列表。

All works beautifully, but valgrind is telling me I have a "definitely lost" at the line where I am calloc'ing the struct item.

所有的作品都很精美,但是valgrind告诉我,我在构造项目的句子中有一个“绝对丢失”。

Now, I'm new to C, but am familiar with Objective-C. In pre-ARC days, you could alloc an object, put it in an array, then release it. The array would retain the object. Once you needed to pull the item out of the array, you would need to retain it first. Otherwise, it would be lost quickly as the array would no longer be retaining the object.

现在,我是C的新手,但我熟悉Objective-C。在ARC之前的日子里,您可以分配一个对象,将其放入一个数组中,然后释放它。该数组将保留该对象。一旦您需要将项目拉出阵列,您需要先保留它。否则,它将很快丢失,因为阵列将不再保留该对象。

So, I guess my question is: how do I get an array/linked list to retain its "objects", so I can free the alloc'd item after entering it into the array or linked list?

所以,我想我的问题是:我如何获得一个数组/链表以保留其“对象”,所以我可以在将它输入数组或链表后释放alloc'd项?

// global array
struct_item** hashtable;

// declare struct
typedef struct struct_item
{
    char item1[SIZE + 1];
    struct struct_item* next_item;
}

// later... dynamically allocate hashtable
hashtable = calloc(sizevar, sizeof(struct_item);

// later, inside of a loop...
// alloc struct item to go into array/linked list (valgrind error happening here)
struct_item* an_item = calloc(1, sizeof(struct_item);

// after setting appropriate values in 'an_item', add struct to hashtable
hashtable[0] = an_item;

// after all is said and done, free the current (remaining) struct_item
// (edit to fix: wasn't struct_item; should've been an_item)
//free(struct_item);
free(an_item);

I'm not freeing any of the other struct_items calloc'd during the loop; just the last one. But, as soon as I free that one, it's value disappears from the hashtable.

我没有在循环中释放任何其他struct_items calloc'd;只是最后一个。但是,只要我释放那个,它的值就会从哈希表中消失。

Ideally, I suppose I'd be freeing each one of the calloc'd struct_items, yes? Valgrind is only reporting one issue, located at the line where I'm calloc'ing 'an_item'.

理想情况下,我想我会释放每个calloc'd struct_items,是吗? Valgrind只报告一个问题,位于我正在调用'an_item'的行。

So, again, how do I get the calloc'd items to stay associated with the hashtable, so I can free 'an_item' without it's data disappearing from the table? What am I missing???

那么,再次,我如何让calloc'd项与哈希表保持关联,这样我可以释放'an_item'而不会从表中消失数据?我错过了什么?

Edit to add note: I do free the struct_items when I'm unloading the hashtable/linked list!

编辑添加注释:当我卸载哈希表/链表时,我释放了struct_items!

2 个解决方案

#1


1  

Found it!

找到了!

I was freeing everything from the linked list during the unload - but not the actual item at the current hashtable index!

我在卸载期间释放了链表中的所有内容 - 但不是当前哈希表索引中的实际项目!

Once I freed hashtable[i] correctly, I got the happy report:

一旦我正确地释放了哈希表[i],我收到了快乐的报告:

==6759== 
==6759== HEAP SUMMARY:
==6759==     in use at exit: 0 bytes in 0 blocks
==6759==   total heap usage: 143,130 allocs, 143,130 frees, 14,883,824 bytes allocated
==6759== 
==6759== All heap blocks were freed -- no leaks are possible
==6759== 
==6759== For counts of detected and suppressed errors, rerun with: -v
==6759== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

#2


0  

This line is problematic

这条线存在问题

// after all is said and done, free the current (remaining) struct_item
free(struct_item);

Change to as 'an_item' contains address returned by the calloc

更改为'an_item'包含calloc返回的地址

  free(an_item);

#1


1  

Found it!

找到了!

I was freeing everything from the linked list during the unload - but not the actual item at the current hashtable index!

我在卸载期间释放了链表中的所有内容 - 但不是当前哈希表索引中的实际项目!

Once I freed hashtable[i] correctly, I got the happy report:

一旦我正确地释放了哈希表[i],我收到了快乐的报告:

==6759== 
==6759== HEAP SUMMARY:
==6759==     in use at exit: 0 bytes in 0 blocks
==6759==   total heap usage: 143,130 allocs, 143,130 frees, 14,883,824 bytes allocated
==6759== 
==6759== All heap blocks were freed -- no leaks are possible
==6759== 
==6759== For counts of detected and suppressed errors, rerun with: -v
==6759== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

#2


0  

This line is problematic

这条线存在问题

// after all is said and done, free the current (remaining) struct_item
free(struct_item);

Change to as 'an_item' contains address returned by the calloc

更改为'an_item'包含calloc返回的地址

  free(an_item);