[英]Memory Leak, but I can't find a way to write the code without a memory leak in C
[英]C memory leak that i can't find at all
因此,該程序一切正常,但我遇到了非常煩人的內存泄漏。 我在電腦前坐了幾個小時,可以弄清楚。
我們有兩個很簡單的實現方式,一個結構是一個雙鏈表,一個是存儲該雙鏈表的哈希表。
現在,我將關鍵字和數據插入到雙向鏈接列表中,這里是函數。
void htable_insert(htable* ht, int key, int data) {
// TODO: Insert a new entry with the given key and data
// Overwrite the old data if the key already exists, duplicate keys are not allowed
ht_entry *new_node;
ht_entry *head;
ht_entry *it;
int sameKey;
int bucketPosition;
new_node = (ht_entry*)malloc(1*sizeof(ht_entry));
bucketPosition = key % ht->size;
sameKey = 0;
for(it = ht->entries[bucketPosition]; it != NULL; it = it->next)
{
if(it->key == key) {
it->data = data;
sameKey = 1;
free(new_node);
new_node = NULL;
break;
}
}
if(!sameKey && new_node) {
head = ht->entries[bucketPosition];
if (head == NULL) {
new_node->key = key;
new_node->data = data;
new_node->next = head;
new_node->prev = NULL;
ht->entries[bucketPosition] = new_node;
new_node = NULL;
} else {
new_node->key = key;
new_node->data = data;
new_node->next = head;
// new_node->prev = head;
head->prev = new_node;
head = new_node;
ht->entries[bucketPosition] = head;
}
}
// free(new_node);
new_node = NULL;
printf("%s\n %d", "INSERT:", key);
for(it = ht->entries[bucketPosition]; it != NULL; it = it->next){
printf("it->key: %d\nit->data: %d\n", it->key, it->data);
}
printf("%s\n", "-------------------------------");
}
這是我的valgrind消息:
==10692== Memcheck, a memory error detector
==10692== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==10692== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==10692== Command: ./chain_hash_table.out
==10692==
==10692==
==10692== HEAP SUMMARY:
==10692== in use at exit: 72 bytes in 3 blocks
==10692== total heap usage: 10 allocs, 7 frees, 376 bytes allocated
==10692==
==10692== 24 bytes in 1 blocks are definitely lost in loss record 2 of 3
==10692== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10692== by 0x4007EE: htable_insert (htable.c:53)
==10692== by 0x400BD2: main (main.c:14)
==10692==
==10692== 48 (24 direct, 24 indirect) bytes in 1 blocks are definitely lost in loss record 3 of 3
==10692== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10692== by 0x4007EE: htable_insert (htable.c:53)
==10692== by 0x400C25: main (main.c:18)
==10692==
==10692== LEAK SUMMARY:
==10692== definitely lost: 48 bytes in 2 blocks
==10692== indirectly lost: 24 bytes in 1 blocks
==10692== possibly lost: 0 bytes in 0 blocks
==10692== still reachable: 0 bytes in 0 blocks
==10692== suppressed: 0 bytes in 0 blocks
==10692==
==10692== For counts of detected and suppressed errors, rerun with: -v
==10692== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
我所知道的始終是第一次插入表,這就是為什么它在第一次插入后在main(18)行上顯示其余內容的原因。
謝謝你們的時間和幫助:)
檢查循環中的break語句,它在第一次迭代時中斷,並且不取消分配節點。
中斷應放在for循環內。
如果要在C程序中分配空間,除非最后釋放所有內容,否則最終將導致內存泄漏。
對於您當前的程序,我假設您最終無法正確釋放。 因此,問題不在htable_insert
函數之內,而在您的釋放/清除函數之內。
如果您在htable_insert
釋放鏈表節點,那么您將無法在程序的其余部分中訪問它們,並且會遇到段錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.