c-如何正确释放哈希表上的元素
我正在做一个程序,其中有一个哈希表,其中的元素具有这种类型的结构c-如何正确释放哈希表上的元素,c,memory-leaks,hashtable,valgrind,C,Memory Leaks,Hashtable,Valgrind,我正在做一个程序,其中有一个哈希表,其中的元素具有这种类型的结构 typedef struct _msg_list{ message_t *msg; struct _msg_list *next; }msg_list; typedef struct _hash_elem{ char *nickname; int nmsg; msg_list *msg_head; msg_list *msg_corr; }hash_elem; 其中,msg_li
typedef struct _msg_list{
message_t *msg;
struct _msg_list *next;
}msg_list;
typedef struct _hash_elem{
char *nickname;
int nmsg;
msg_list *msg_head;
msg_list *msg_corr;
}hash_elem;
其中,msg_list是指向已接收消息列表的指针。消息具有这种结构
typedef struct {
int op;
char sender[33];
} message_hdr_t;
typedef struct {
char receiver[33];
unsigned int len;
} message_data_hdr_t;
typedef struct {
message_data_hdr_t hdr;
char *buf;
} message_data_t;
typedef struct {
message_hdr_t hdr;
message_data_t data;
} message_t;
当我调用freeHashData函数来清理内存时,valgrind给了我这个输出
==4709== Memcheck, a memory error detector
==4709== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4709== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==4709== Command: ./chatty
==4709==
==4709==
==4709== HEAP SUMMARY:
==4709== in use at exit: 98 bytes in 4 blocks
==4709== total heap usage: 14 allocs, 10 frees, 8,622 bytes allocated
==4709==
==4709== 16 bytes in 1 blocks are indirectly lost in loss record 1 of 4
==4709== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4709== by 0x108F0A: insertMsg (in /home/giacomo/Scrivania/chatty)
==4709== by 0x1092DB: main (in /home/giacomo/Scrivania/chatty)
==4709==
==4709== 32 (16 direct, 16 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 4
==4709== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4709== by 0x108F0A: insertMsg (in /home/giacomo/Scrivania/chatty)
==4709== by 0x1092C8: main (in /home/giacomo/Scrivania/chatty)
==4709==
==4709== 33 bytes in 1 blocks are definitely lost in loss record 3 of 4
==4709== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4709== by 0x1090F4: main (in /home/giacomo/Scrivania/chatty)
==4709==
==4709== 33 bytes in 1 blocks are definitely lost in loss record 4 of 4
==4709== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4709== by 0x109166: main (in /home/giacomo/Scrivania/chatty)
==4709==
==4709== LEAK SUMMARY:
==4709== definitely lost: 82 bytes in 3 blocks
==4709== indirectly lost: 16 bytes in 1 blocks
==4709== possibly lost: 0 bytes in 0 blocks
==4709== still reachable: 0 bytes in 0 blocks
==4709== suppressed: 0 bytes in 0 blocks
该程序只需在哈希表中注册2个用户,然后我向一个用户发送消息并将其存储在消息列表中
这是主要功能和其他功能
typedef struct icl_entry_s {
void* key;
void *data;
struct icl_entry_s* next;
} icl_entry_t;
typedef struct icl_hash_s {
int nbuckets;
int nentries;
icl_entry_t **buckets;
unsigned int (*hash_function)(void*);
int (*hash_key_compare)(void*, void*);
} icl_hash_t;
icl_hash_t *hash;
static inline void setHeader(message_hdr_t *hdr, int op, char *sender) {
#if defined(MAKE_VALGRIND_HAPPY)
memset((char*)hdr, 0, sizeof(message_hdr_t));
#endif
hdr->op = op;
strncpy(hdr->sender, sender, strlen(sender)+1);
}
static inline void setData(message_data_t *data, char *rcv, const char *buf, unsigned int len) {
#if defined(MAKE_VALGRIND_HAPPY)
memset((char*)&(data->hdr), 0, sizeof(message_data_hdr_t));
#endif
strncpy(data->hdr.receiver, rcv, strlen(rcv)+1);
data->hdr.len = len;
data->buf = (char *)buf;
}
/**
* A simple string hash.
*
* An adaptation of Peter Weinberger's (PJW) generic hashing
* algorithm based on Allen Holub's version. Accepts a pointer
* to a datum to be hashed and returns an unsigned integer.
* From: Keith Seymour's proxy library code
*
* @param[in] key -- the string to be hashed
*
* @returns the hash index
*/
unsigned int
hash_pjw(void* key)
{
char *datum = (char *)key;
unsigned int hash_value, i;
if(!datum) return 0;
for (hash_value = 0; *datum; ++datum) {
hash_value = (hash_value << ONE_EIGHTH) + *datum;
if ((i = hash_value & HIGH_BITS) != 0)
hash_value = (hash_value ^ (i >> THREE_QUARTERS)) & ~HIGH_BITS;
}
return (hash_value);
}
static int string_compare(void* a, void* b)
{
return (strcmp( (char*)a, (char*)b ) == 0);
}
/**
* Create a new hash table.
*
* @param[in] nbuckets -- number of buckets to create
* @param[in] hash_function -- pointer to the hashing function to be used
* @param[in] hash_key_compare -- pointer to the hash key comparison function to be used
*
* @returns pointer to new hash table.
*/
icl_hash_t *
icl_hash_create( int nbuckets, unsigned int (*hash_function)(void*), int (*hash_key_compare)(void*, void*) )
{
icl_hash_t *ht;
int i;
ht = (icl_hash_t*) malloc(sizeof(icl_hash_t));
if(!ht) return NULL;
ht->nentries = 0;
ht->buckets = (icl_entry_t**)malloc(nbuckets * sizeof(icl_entry_t*));
if(!ht->buckets) return NULL;
ht->nbuckets = nbuckets;
for(i=0;i<ht->nbuckets;i++)
ht->buckets[i] = NULL;
ht->hash_function = hash_function ? hash_function : hash_pjw;
ht->hash_key_compare = hash_key_compare ? hash_key_compare : string_compare;
return ht;
}
/**
* Search for an entry in a hash table.
*
* @param ht -- the hash table to be searched
* @param key -- the key of the item to search for
*
* @returns pointer to the data corresponding to the key.
* If the key was not found, returns NULL.
*/
void *
icl_hash_find(icl_hash_t *ht, void* key)
{
icl_entry_t* curr;
unsigned int hash_val;
if(!ht || !key) return NULL;
hash_val = (* ht->hash_function)(key) % ht->nbuckets;
for (curr=ht->buckets[hash_val]; curr != NULL; curr=curr->next)
if ( ht->hash_key_compare(curr->key, key))
return(curr->data);
return NULL;
}
/**
* Insert an item into the hash table.
*
* @param ht -- the hash table
* @param key -- the key of the new item
* @param data -- pointer to the new item's data
*
* @returns pointer to the new item. Returns NULL on error.
*/
icl_entry_t *
icl_hash_insert(icl_hash_t *ht, void* key, void *data)
{
icl_entry_t *curr;
unsigned int hash_val;
if(!ht || !key) return NULL;
hash_val = (* ht->hash_function)(key) % ht->nbuckets;
for (curr=ht->buckets[hash_val]; curr != NULL; curr=curr->next)
if ( ht->hash_key_compare(curr->key, key))
return(NULL); /* key already exists */
/* if key was not found */
curr = (icl_entry_t*)malloc(sizeof(icl_entry_t));
if(!curr) return NULL;
curr->key = key;
curr->data = data;
curr->next = ht->buckets[hash_val]; /* add at start */
ht->buckets[hash_val] = curr;
ht->nentries++;
return curr;
/**
* Free hash table structures (key and data are freed using functions).
*
* @param ht -- the hash table to be freed
* @param free_key -- pointer to function that frees the key
* @param free_data -- pointer to function that frees the data
*
* @returns 0 on success, -1 on failure.
*/
int
icl_hash_destroy(icl_hash_t *ht, void (*free_key)(void*), void (*free_data)(void*))
{
icl_entry_t *bucket, *curr, *next;
int i;
if(!ht) return -1;
for (i=0; i<ht->nbuckets; i++) {
bucket = ht->buckets[i];
for (curr=bucket; curr!=NULL; ) {
next=curr->next;
if (*free_key && curr->key) (*free_key)(curr->key);
if (*free_data && curr->data) (*free_data)(curr->data);
free(curr);
curr=next;
}
}
if(ht->buckets) free(ht->buckets);
if(ht) free(ht);
return 0;
}
int insertMsg(char* client_nickname,message_t *msg){
char* buf=malloc(msg->data.hdr.len);
strncpy(buf,msg->data.buf,msg->data.hdr.len);
hash_elem *user = icl_hash_find(hash, client_nickname);
if(user->nmsg == 32){
return -1;
}
msg_list *mex=malloc(sizeof(msg_list));
if(mex==NULL){
return -1;
}
mex->msg=malloc(sizeof(message_t));
if(mex->msg==NULL){
return -1;
}
mex->msg->data.buf=NULL;
setHeader(&mex->msg->hdr,2,msg->hdr.sender);
setData(&mex->msg->data,msg->data.hdr.receiver,buf,msg->data.hdr.len);
mex->next=NULL;
if(user->msg_head==NULL){
user->msg_head=mex;
user->msg_corr=user->msg_head;
}else{
user->msg_corr->next=mex;
user->msg_corr=user->msg_corr->next;
}
user->nmsg++;
return 0;
}
void freeHashData(void* data){
hash_elem* data2=(hash_elem*) data;
msg_list* tmp=data2->msg_head;
while(tmp!=NULL){
free(tmp->msg->data.buf);
free(tmp->msg);
data2->msg_head=data2->msg_head->next;
tmp=data2->msg_head;
}
//free(data2);
free(data);
free(tmp);
}
int main(int argc, char *argv[]) {
//tabelle hash
hash = icl_hash_create(1024, &hash_pjw, NULL); //rivedere 1024 forse troppo
//fai insert per registrazioni
hash_elem *user1 = malloc(sizeof(hash_elem));
user1->nickname=malloc(33);
strncpy(user1->nickname,"user1",33);
user1->nmsg=0;
user1->msg_head=NULL;
user1->msg_corr=NULL;
hash_elem *user2 = malloc(sizeof(hash_elem));
user2->nickname=malloc(33);
strncpy(user2->nickname,"user2",33);
user2->nmsg=0;
user2->msg_head=NULL;
user2->msg_corr=NULL;
icl_hash_insert(hash, "user1", (void*)user1);
icl_hash_insert(hash, "user2", (void*)user2);
message_t msg;
memset(&msg,0,sizeof(message_t));
msg.data.buf=NULL;
message_t msg2;
memset(&msg2,0,sizeof(message_t));
msg2.data.buf=NULL;
setHeader(&msg.hdr,1,"SERVER");
setHeader(&msg2.hdr,1,"SERVER");
setData(&msg.data,"user2","hello",strlen("hello")+1);
setData(&msg2.data,"user2","hello",strlen("hello")+1);
insertMsg("user2",&msg);
insertMsg("user2",&msg2);
icl_hash_destroy(hash, NULL, freeHashData);
return 0;
}
typedef结构icl\u条目{
无效*键;
作废*数据;
结构icl\u条目\u s*下一步;
}icl_条目;
类型定义结构icl_散列{
国际nbuckets;
内部要素;
icl\u条目**桶;
无符号整数(*hash_函数)(void*);
int(*哈希键比较)(void*,void*);
}icl_hash_t;
icl_hash_t*hash;
静态内联void setHeader(message_hdr_t*hdr、int op、char*sender){
#如果定义了(使你高兴)
memset((char*)hdr,0,sizeof(message_hdr_t));
#恩迪夫
hdr->op=op;
strncpy(hdr->sender,sender,strlen(sender)+1);
}
静态内联void setData(message_data_t*data,char*rcv,const char*buf,unsigned int len){
#如果定义了(使你高兴)
memset((char*)&(data->hdr),0,sizeof(message_data_hdr_t));
#恩迪夫
strncpy(数据->hdr.receiver,rcv,strlen(rcv)+1);
数据->hdr.len=len;
数据->buf=(字符*)buf;
}
/**
*一个简单的字符串散列。
*
*Peter Weinberger(PJW)泛型哈希的改编
*基于Allen Holub版本的算法。接受指针
*到要散列的数据,并返回无符号整数。
*发件人:Keith Seymour的代理库代码
*
*@param[in]key——要散列的字符串
*
*@返回哈希索引
*/
无符号整型
hash_pjw(void*键)
{
字符*数据=(字符*)键;
无符号整数散列值,i;
如果(!datum)返回0;
对于(散列值=0;*datum;++datum){
哈希值=(哈希值>四分之三))&~高位;
}
返回值(散列值);
}
静态整数字符串_比较(void*a,void*b)
{
返回(strcmp((char*)a,(char*)b)==0;
}
/**
*创建一个新的哈希表。
*
*@param[in]nbuckets—要创建的存储桶数
*@param[in]散列函数——指向要使用的散列函数的指针
*@param[in]hash_key_compare——指向要使用的哈希键比较函数的指针
*
*@返回指向新哈希表的指针。
*/
icl\u哈希\t*
icl_哈希_创建(int-nbuckets,unsigned int(*hash_函数)(void*),int(*hash_键比较)(void*,void*))
{
icl_hash_t*ht;
int i;
ht=(icl_hash_t*)malloc(sizeof(icl_hash_t));
如果(!ht)返回NULL;
ht->nentries=0;
ht->bucket=(icl_entry_t**)malloc(nbuckets*sizeof(icl_entry_t*));
如果(!ht->bucket)返回空值;
ht->nbuckets=nbuckets;
对于(i=0;inbuckets;i++)
ht->bucket[i]=NULL;
ht->hash_函数=hash_函数?hash_函数:hash_pjw;
ht->hash\u key\u compare=hash\u key\u compare?hash\u key\u compare:字符串\u compare;
返回ht;
}
/**
*在哈希表中搜索条目。
*
*@param ht--要搜索的哈希表
*@param key--要搜索的项目的键
*
*@返回指向键对应的数据的指针。
*如果找不到密钥,则返回NULL。
*/
空虚*
icl_哈希查找(icl_哈希*ht,void*键)
{
icl\u条目\u t*当前;
无符号整数散列值;
如果(!ht | |!key)返回NULL;
hash_val=(*ht->hash_函数)(键)%ht->nbuckets;
对于(curr=ht->bucket[hash_val];curr!=NULL;curr=curr->next)
if(ht->hash\u key\u compare(curr->key,key))
返回(当前->数据);
返回NULL;
}
/**
*将项目插入哈希表。
*
*@param ht——哈希表
*@param key——新项目的密钥
*@param data--指向新项数据的指针
*
*@返回指向新项目的指针。错误时返回NULL。
*/
icl\u条目\u t*
icl_哈希插入(icl_哈希t*ht、void*键、void*数据)
{
icl\u条目\u t*当前;
无符号整数散列值;
如果(!ht | |!key)返回NULL;
hash_val=(*ht->hash_函数)(键)%ht->nbuckets;
对于(curr=ht->bucket[hash_val];curr!=NULL;curr=curr->next)
if(ht->hash\u key\u compare(curr->key,key))
return(NULL);/*键已存在*/
/*如果找不到密钥*/
curr=(icl_entry_t*)malloc(sizeof(icl_entry_t));
如果(!curr)返回空值;
当前->键=键;
当前->数据=数据;
curr->next=ht->bucket[hash_val];/*开始添加*/
ht->bucket[hash_val]=curr;
ht->nentries++;
返回货币;
/**
*自由哈希表结构(使用函数释放键和数据)。
*
*@param ht--要释放的哈希表
*@param free_key——指向释放该键的函数的指针
*@param free_data——指向释放数据的函数的指针
*
*@成功返回0,失败返回1。
*/
int
icl_哈希_销毁(icl_哈希_t*ht,void(*自由密钥)(void*),void(*自由数据)(void*))
{
icl_条目*bucket、*curr、*next;
int i;
如果(!ht)返回-1;
对于(i=0;inbuckets;i++){
bucket=ht->bucket[i];
for(curr=bucket;curr!=NULL;){
下一步=当前->下一步;
如果(*free_key&&curr->key)(*free_key)(curr->key);
如果(*自由_数据&当前->数据)(*自由_数据)(当前->数据);
免费(货币);
curr=next;
}
}
如果(ht->铲斗)空闲(ht->铲斗);
如果(ht)自由(ht);
返回0;
}
int insertMsg(字符*客户端\昵称,消息\ t*msg){
char*buf=malloc(msg->data.hdr.len);
strncpy(buf,msg->data.buf,msg->data.hdr.len);
hash_elem*user=icl_hash_find(散列,客户端昵称);
如果(用户->nmsg==32){
返回-1;
}
msg_list*mex=malloc(sizeof(msg_list));
如果(mex==NULL){
返回-1;
}
mex->msg=malloc(sizeof(message_t));
如果(mex->msg==NULL){
返回-1;
}
mex->msg->data.buf=NULL;
setHeader(&mex->msg->hdr,2,msg->hdr.sender);
setData(&mex->msg->data,msg->data.hdr.receiver,buf,msg->data.hdr.len);
mex->nex
void freeHashData(void* data){
hash_elem* data2=(hash_elem*) data;
msg_list* tmp=data2->msg_head;
while (tmp != NULL) {
msg_list* next = tmp->next;
free(tmp->msg->data.buf);
free(tmp->msg);
free(tmp); /* you missed that */
tmp = next;
}
free(data2->nickname); /* you missed that */
free(data2);
}