EDIT: solved, I know how but I don't understand why.
I changed variables
declaration from
tr1::unordered_map<int,T> variables;
to
unordered_map<int,T> variables;
and it's work fine.
If you know why please write it in the answers.
I have a very large program, so I don't know which code I should bring here.
There is abstract class, that inherits with derived class.
The abstract have unordered_map<int,int>
(template) as private member, and public method insert(int,int)
.
The derived class use the base class insert
method to insert elements to the unordered_map<int,int>
container,
The first int
uses like counter and start with 0. The first eleven insert elements going O.K. but in the 12th element I get sigsegv,and fault in struct equal_to
at stl_function.h(209).
In the debugger I have saw that the unordered_map's bucket_count equal to 11, maybe it's clue for something.
My compiler is gcc 4.6.1.
Maybe you can write in general what can cause sigsegv in unordered_map.insert
?
Thank you, and sorry about my poor English.
I will bring specific code, if I know which.
EDIT:
This is the insert
method:
virtual void Insert(int arrayPlace, T value)
{
if (!isReadOnly)
{
if (IsValueDataValid(value))
{
variables[arrayPlace] = value;
}
else
{
throw 2;
}
}
else
{
throw 4;
}
};
The declaration is:
tr1::unordered_map<int,T> variables;
The sigsegv happend when arrayPlace
==11, And it dosn't matter what value
equal.
Internally unordered_map is implemented using Hash Table, the key provided to map is hashed into indices of a hash table which is why the performance of data structure depends on the hash function a lot but on average, the cost of search, insert, and delete from the hash table is O(1).
unordered_map is a data structure that is used to store data in the form of pairs of keys and their corresponding values. Unordered_map uses a hashing function to store a key-value pair, due to which the average time complexity for searching a key-value pair becomes O(1).
reserve(1024); mp. max_load_factor(0.25); With this two lines unordered_map become about 10 times faster. You can replace 1024 with another suitable power of two.
The unordered_map::clear() function is available in the <unordered_map> header file in C++. The unordered_map::clear() removes all the elements from the unordered map. In other words, it empties the unordered map.
The answer to the question is very simple: if you use the code correctly, no segmentation fault will be created by std::unordered_map
! So the question becomes: what are typical user errors when using std::unordered_map
? Off-hand I would think immediately of three issues:
T
you got correctly implements copy construction. In particular, you want to pay attention to the copy constructor if it isn't in the class but the class has an assignment operator or a destructor.Given that the key is an int
and the hash code and equality are provided I would concentrate on the first issue. That is, I would concentrate on this once I have proved that the use of the std::unordered_map
is indeed the problem: the segmentation violation may also quite easily result from things being messed up earlier. For example, something may have overwritten memory or deleted memory the wrong way, etc. Tools like purify or valgrind can help finding these issue. In any case, you want to boil down the program to a minimal crashing example. Typically I find that the issue becomes obvious in the process.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With