我試圖構建一個包含ints,time_t和幾個char *的struct的實例的GHashTable。插入非pod結構到GHashTable中
我的問題是,你如何將一個結構的實例插入GHashTable?有很多關於如何插入字符串或int的例子(分別使用g_str_hash和g_int_hash),但我猜想我想使用g_direct_hash,而且我似乎無法找到任何示例。
理想的情況下,我的代碼是這樣的:
GHashtable table;
table = g_hash_table_new(g_direct_hash, g_direct_equal);
struct mystruct;
mystruct.a = 1;
mystruct.b = "hello";
mystruct.c = 5;
mystruct.d = "test";
g_hash_table_insert(table,mystruct.a,mystruct);
顯然,這是不正確的,它不會編譯。任何人都可以提供一個可以做我想做的事情的例子嗎? 謝謝, 裏克
'表= g_hash_table_new_full(g_int_hash,g_int_equal,NULL,g_free);' – ntd 2010-04-20 20:05:46
@ntd:謝謝,固定! – unwind 2010-04-21 06:43:33
一個改進是使用g_new(struct mystruct,1)而不是g_malloc。刪除兩個錯誤來源(分配錯誤的大小,並將內存分配給錯誤的類型)。 – 2010-11-01 14:17:07