首先,我插入 key=str1,value=header_buff
二、我用str2查找指针
第三,我释放了我 malloc 的指针,但失败了。为什么?
#include <stdio.h>
#include <glib.h>
int main()
{
GHashTable *hash; ///define my hashtable
char str1[20] = "key";
char str2[20] = "key";
char *header_buff = (char*) malloc(sizeof(char) * 1024 * 1024);
memcpy(header_buff, "value", 5);
printf("%p\n", header_buff);
hash = g_hash_table_new(g_str_hash, g_direct_hash); ///here I use (g_str_hash, g_direct_hash)
g_hash_table_insert(hash, str1, header_buff); /// insert the str1 as key
char * c = (char*) g_hash_table_lookup(hash, str2); ///use str2 to find the value
if (c)
{
printf("%s\n", c); ///can print the string "value"
printf("%p\n", c);
free(c); ///the same address? but I can't free the mem!
c = NULL;
}
return 0;
}