Glib 哈希表存储了不正确的键

发布于 2024-12-10 05:43:31 字数 959 浏览 0 评论 0原文

我是第一次使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

来日方长 2024-12-17 05:43:31

dump_pair() 中取消引用关键指针怎么样?

g_print ("Key: %u Value: %s\n", *key, value);

What about dereferencing the key pointer in dump_pair()?

g_print ("Key: %u Value: %s\n", *key, value);
琴流音 2024-12-17 05:43:31

当您在插入调用中复制 num_id 变量时,是否应该获取其地址?

g_memdup(num_id, sizeof(num_id))

应该是

g_memdup(&num_id, sizeof(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.

g_memdup(num_id, sizeof(num_id))

should be

g_memdup(&num_id, sizeof(num_id))

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.

笑红尘 2024-12-17 05:43:31

您使用 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 declared g_hash_table_new(g_int64_hash, g_int64_equal) with 64-bit keys. Why don't you re-declare it as g_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文