谷歌的dense_hash_map在set_empty_key()函数中崩溃

发布于 2024-11-09 15:41:12 字数 319 浏览 4 评论 0原文

我正在尝试使用 google dend_hash_map 来存储键值数据而不是 std:map。

当我使用 (int, int ) 对进行测试时,我设置了 set_empty_key(mymap, -2) 并且它起作用了。

但是,现在当我将它与我的 (hash, value) 对一起使用时,我设置了 set_empty_key (mymap -2) 或 set_empty_key(mymap, some_random_hash),在这两种情况下我的程序都会在 set_empty_key(); 中崩溃。

任何人都可以指导我吗?我该如何修复这个崩溃?

谢谢。

I am trying to use google dense_hash_map to store key value data instead of std:map.

When I tested with (int, int ) pair, I set the set_empty_key(mymap, -2) and it worked.

But, now when I use it with my (hash, value) pair, I set the set_empty_key (mymap -2) or set_empty_key(mymap, some_random_hash), in both the cases my program crashes in set_empty_key();.

Anyone can guide me with this? How can I fix this crash?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

羁绊已千年 2024-11-16 15:41:12

我不知道您崩溃的确切原因,但是根据您的描述,我至少看到两个潜在的错误。

第一的。检查 key_typedata_type 类型是否都是 POD 类型,并且不包含指向自身的指针。更具体地说(原始):

key_type 和 data_type 都必须是
普通的旧数据。此外,还应该有
没有指向的数据结构
直接进入键或值的部分,
包括键或值本身(对于
例如,你不能有这样的值
结构体{int a = 1, *b = &a}。这是
因为dense_hash_map使用malloc()
和 free() 分配空间
键和值,以及 memmove() 到
重新组织键和值
内存。

第二。关于使用dense_hash_map。您需要设置一些特殊的“空”键值,该值永远不会用于存储在集合中的真实元素。此外,如果您要使用erase(),您需要为已删除的项目指定特殊的密钥,该密钥也永远不会用作实际存储的项目的密钥。
这是此处完美描述的:

dense_hash_map 需要您调用
紧接着 set_empty_key()
构造哈希图,以及之前
调用任何其他dense_hash_map
方法。 (这是最大的
dend_hash_map 之间的区别
API 和其他哈希映射 API。看
原因.implementation.html
必要的。)论点
set_empty_key() 应该是一个键值
从未用于合法目的
哈希映射条目。如果你没有这样的
键值,您将无法使用
密集哈希映射。调用时出错
insert() 插入一个项目,其键为
“空钥匙。” ensemble_hash_map 也
要求您调用 set_deleted_key()
在调用erase()之前。论据
set_deleted_key() 应该是
从未使用过的键值
合法的哈希映射条目。它必须
与使用的键值不同
对于set_empty_key()。这是一个错误
调用erase()而不先调用
set_deleted_key(),它也是一个
使用项目调用 insert() 时出错
其密钥是“已删除的密钥”。

I don't know the exact reason of crash you've got, but, based on your description I see at least two potential mistakes.

First. Check that both key_type and data_type types are POD types and don't contain pointers to itself. More specifically (original):

Both key_type and data_type must be
plain old data. In addition, there should
be no data structures that point
directly into parts of key or value,
including the key or value itself (for
instance, you cannot have a value like
struct {int a = 1, *b = &a}. This is
because dense_hash_map uses malloc()
and free() to allocate space for the
key and value, and memmove() to
reorganize the key and value in
memory.

Second. Concerning using dense_hash_map. You need to set up some special "empty" key value which will never be used for real elements stored in your collection. Moreover if you are going to use erase() you need to specify special key for deleted items which also will never be used as key for real stored items.
That is perfectly described here:

dense_hash_map requires you call
set_empty_key() immediately after
constructing the hash-map, and before
calling any other dense_hash_map
method. (This is the largest
difference between the dense_hash_map
API and other hash-map APIs. See
implementation.html for why this is
necessary.) The argument to
set_empty_key() should be a key-value
that is never used for legitimate
hash-map entries. If you have no such
key value, you will be unable to use
dense_hash_map. It is an error to call
insert() with an item whose key is the
"empty key." dense_hash_map also
requires you call set_deleted_key()
before calling erase(). The argument
to set_deleted_key() should be a
key-value that is never used for
legitimate hash-map entries. It must
be different from the key-value used
for set_empty_key(). It is an error to
call erase() without first calling
set_deleted_key(), and it is also an
error to call insert() with an item
whose key is the "deleted key."

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