Glib 哈希表存储了不正确的键
我是第一次使用 glib,并且在使用哈希表时遇到了一些问题。我正在尝试使用 uint32_t 作为键。
GHashTable *fwd_table = g_hash_table_new(g_int64_hash, g_int64_equal);
// some code here
while(something is true) {
uint32_t net_addr = ip_strtoint(addr) - net_mask(addr); // value of the key
printf("The net_addr is %u\n",net_addr);
g_hash_table_insert(fwd_table, g_memdup(&net_addr, sizeof(net_addr)),
g_memdup(num_id, sizeof(num_id)));
}
void dump_pair (const uint32_t *key, const char *value) {
g_print ("Key: %u Value: %s\n", key, value);
}
g_hash_table_foreach (fwd_table, (GHFunc)dump_pair, NULL);
输出为:
The net_addr is 3232301056
The net_addr is 3232251904
The net_addr is 3232284672
The net_addr is 3232251686
The net_addr is 3372220416
Key: 6307440 Value: 1
Key: 6307536 Value: 2
Key: 6307728 Value: 5
Key: 6307344 Value: 3
Key: 6307632 Value: 7
键应对应于 net_addr。你知道我在这里做错了什么吗?
I'm using glib for the first time and am having some trouble with a hash table. I'm trying to use uint32_t as the keys.
GHashTable *fwd_table = g_hash_table_new(g_int64_hash, g_int64_equal);
// some code here
while(something is true) {
uint32_t net_addr = ip_strtoint(addr) - net_mask(addr); // value of the key
printf("The net_addr is %u\n",net_addr);
g_hash_table_insert(fwd_table, g_memdup(&net_addr, sizeof(net_addr)),
g_memdup(num_id, sizeof(num_id)));
}
void dump_pair (const uint32_t *key, const char *value) {
g_print ("Key: %u Value: %s\n", key, value);
}
g_hash_table_foreach (fwd_table, (GHFunc)dump_pair, NULL);
The output is:
The net_addr is 3232301056
The net_addr is 3232251904
The net_addr is 3232284672
The net_addr is 3232251686
The net_addr is 3372220416
Key: 6307440 Value: 1
Key: 6307536 Value: 2
Key: 6307728 Value: 5
Key: 6307344 Value: 3
Key: 6307632 Value: 7
The keys should correspond to the net_addr. Any ideas what I'm doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
dump_pair()
中取消引用关键指针怎么样?What about dereferencing the key pointer in
dump_pair()
?当您在插入调用中复制
num_id
变量时,是否应该获取其地址?即
应该是
有点难以确定,因为您没有显示 num_id 的类型,但由于该参数对应于表中存储的值,因此它应该是一个指针。
将您的键和值收集到单个
struct
中并插入它们会更有意义。Could it be that the
num_id
variable should have its address taken when you duplicate it in the insert call?I.e.
should be
It's a bit difficult to be sure since you don't show the type of
num_id
, but since that argument corresponds to the value stored in the table, it should be a pointer.It would make more sense to collect your key and value into a single
struct
, and insert those.您使用
uint32_t
作为密钥,但您使用 64 位密钥声明了g_hash_table_new(g_int64_hash, g_int64_equal)
。为什么不将其重新声明为g_hash_table_new(g_int32_hash, g_int32_equal)
。如果您仍想按原样使用它,则需要在复制新值之前将密钥的所有 64 位清零。
You are using
uint32_t
as a key and yet you declaredg_hash_table_new(g_int64_hash, g_int64_equal)
with 64-bit keys. Why don't you re-declare it asg_hash_table_new(g_int32_hash, g_int32_equal)
.If you still want to use it as is you would need to zero out all 64 bit of the key before copying a new value in it.