Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google's dense_hash_map crashing in set_empty_key() function

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.

like image 896
PTS Avatar asked May 25 '11 13:05

PTS


1 Answers

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."

like image 192
prokher Avatar answered Nov 13 '22 04:11

prokher