Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C GLib哈希表-无法查找键/值_C_Hashtable_Glib - Fatal编程技术网

C GLib哈希表-无法查找键/值

C GLib哈希表-无法查找键/值,c,hashtable,glib,C,Hashtable,Glib,我试图添加malloc字符串作为键,结构作为值。每次都是一个新字符串,但有时文本是相同的,因此应将它们视为相同的键。添加结构后,我尝试检索它,但没有成功(下面的“添加失败”)。另外,我的总计数没有更新,当销毁调用空闲时,我会查找故障。我已经把免费的评论删掉了 GHashTable *heavyhitter= g_hash_table_new_full(NULL, g_str_equal, (GDestroyNotify) free_hh_key,(GDestroyNotify) free_hh_

我试图添加malloc字符串作为键,结构作为值。每次都是一个新字符串,但有时文本是相同的,因此应将它们视为相同的键。添加结构后,我尝试检索它,但没有成功(下面的“添加失败”)。另外,我的总计数没有更新,当销毁调用空闲时,我会查找故障。我已经把免费的评论删掉了

GHashTable *heavyhitter= g_hash_table_new_full(NULL, g_str_equal, (GDestroyNotify) free_hh_key,(GDestroyNotify) free_hh_metadatarecords);
...
//Called multiple times:
void add_to_heavyhitter_hashtable(struct memory *mem, const char *file,int line, size_t sz) {   
struct hh_metadata_record *hh_metadata_record_toadd;

char *orig_key;
asprintf(&orig_key, "%s:%d", file, line);
char *effective_key = orig_key;

gpointer e_key_ptr = &effective_key;
gpointer hh_meta_ptr = &hh_metadata_record_toadd;

if (g_hash_table_lookup_extended(mem->heavyhitter_hashtable, &orig_key,
        &e_key_ptr, &hh_meta_ptr)) {
    printf("found\n");
    hh_metadata_record_toadd = hh_meta_ptr;
    printf("hh_metadata_record_toadd sz: %Zu\n", hh_metadata_record_toadd->sz);
} else {
    printf("not found\n");
    hh_metadata_record_toadd = hh_get_metadata_record_new(sz);

}

printf("Effective Key: %s\n", effective_key);

hh_metadata_record_toadd->count += 1;
hh_metadata_record_toadd->sz += sz;
mem->hh_total_count += 1;
mem->hh_total_size += sz;

g_hash_table_insert(mem->heavyhitter_hashtable, e_key_ptr,
        hh_metadata_record_toadd);
if(g_hash_table_lookup_extended(mem->heavyhitter_hashtable, effective_key, NULL, NULL ))
    printf("Succesfully added.\n");
else
    printf("Failed add\n");
printf("hash Table size: %Zu\n",g_hash_table_size(mem->heavyhitter_hashtable));
自由函数现在已被注释掉:

void free_hh_key(gpointer a) {
    (void) a;
    printf("Freeing: %s\n", (char*) a);
    //free(a);
}
结果:

not found
Effective Key: hhtest.c:49
Failed add
hash Table size: 1
not found
Effective Key: hhtest.c:49
Freeing: �' �'  �'  �/Y�
Failed add
hash Table size: 1
not found
Effective Key: hhtest.c:10
Freeing: �' �'  �'  �/Y�
Failed add
hash Table size: 1

我将函数类型更改为:g_str_hash

为什么要为hash_func创建带有NULL的哈希表?你的代码很难读取BTW,考虑在问这个问题时在容易阅读的测试用例中隔离问题代码。