我在我的内存结构下的 GLib 哈希表中添加了几个键值对。键是从 malloc 返回的 (char *) 指针。该值是分配的 malloc 大小。
void addstomemstruct(struct memory *mem, char *key_address, size_t sz) {
g_hash_table_insert(mem->singleton, key_address, &sz);
printf("** retrieving size: %Zu add=%p\n",(*(size_t*) g_hash_table_lookup(mem->singleton, key_address)), key_address);
}
后来我根据key去取value的时候,返回的size(value)总是10,这是我最后加到hash表中的值
size_t get_blocksize(GHashTable *hashtable, char *key_address, size_t *result_flag)
{ size_t *sz_ptr = (size_t *) g_hash_table_lookup(hashtable, key_address);
...
char *logmsg;asprintf(&logmsg,"In get_blocksize: *sz_ptr=%Zu\n",*sz_ptr);
log_msg(logmsg);
printf("** retrieving size: %Zu add=%p\n",(*(size_t*) g_hash_table_lookup(hashtable, key_address)), key_address);
...
}
打印出:
preparing to add pointer: 0x9b52f10
** adding size: 1
** retrieving size: 1 add=0x9b52f10
preparing to add pointer: 0x9b52f48
** adding size: 2
** retrieving size: 2 add=0x9b52f48
preparing to add pointer: 0x9b52f58
** adding size: 3
...
preparing to add pointer: 0x9b52fc8
** adding size: 10
** retrieving size: 10 add=0x9b52fc8
然后当我尝试检索第一个值时:
In get_blocksize: *sz_ptr=10
** retrieving size: 10 add=0x9b52f10
Freed address 0x9b52f10. Size: 10. mem->stats->active_count=9, mem->stats->active_size=45.