Std :: unordered_map out-of-range vector index

I use std::unordered_map<void *, size_t> to store some values, and when I add a new value, I get a "vector index out of range". I am using Visual Studio 2012, and error tracing:

 std::vector<std::_List_unchecked_iterator<std::_List_val<std::_List_simple_types<std::pair<void * const,unsigned int> > > >,std::_Wrap_alloc<std::allocator<std::_List_unchecked_iterator<std::_List_val<std::_List_simple_types<std::pair<void * const,unsigned int> > > > > > >::operator[](unsigned int _Pos) Line 1140 C++ std::_Hash<std::_Umap_traits<void *,unsigned int,std::_Uhash_compare<void *,std::hash<void *>,std::equal_to<void *> >,std::allocator<std::pair<void * const,unsigned int> >,0> >::_Vec_lo(unsigned int _Bucket) Line 907 C++ std::_Hash<std::_Umap_traits<void *,unsigned int,std::_Uhash_compare<void *,std::hash<void *>,std::equal_to<void *> >,std::allocator<std::pair<void * const,unsigned int> >,0> >::_End(unsigned int _Bucket) Line 936 C++ std::_Hash<std::_Umap_traits<void *,unsigned int,std::_Uhash_compare<void *,std::hash<void *>,std::equal_to<void *> >,std::allocator<std::pair<void * const,unsigned int> >,0> >::_Insert<std::pair<void * const,unsigned int>,std::_Nil>(std::pair<void * const,unsigned int> && _Val, std::_Nil _Pnode) Line 872 C++ std::_Hash<std::_Umap_traits<void *,unsigned int,std::_Uhash_compare<void *,std::hash<void *>,std::equal_to<void *> >,std::allocator<std::pair<void * const,unsigned int> >,0> >::insert(std::pair<void * const,unsigned int> && _Val) Line 371 C++ 

specificaly:

 _Unchecked_iterator& _Vec_lo(size_type _Bucket) { // return reference to begin() for _Bucket return (_Vec[2 * _Bucket]); } 

where _Vec is the empty vector and _Bucket is the hash pointer (> 0). The same thing happens when the key type is not void *, but uintptr_t. Is this a VS error or am I something wrong?

Note: this question is related to C ++ unorderedmap out-of-range vector index - this is the same problem but the answer is not related

+6
source share
1 answer

This is a rather old question, and I do not know if my answer is appropriate to the question. But since we did not receive any additional information from the OP, and I encountered the same runtime error, I am posting my solution for my specific problem. Maybe someone finds this useful.

In my case, it was a static initialization order task. I accessed the unordered_map (which was a static member of the class) before the static members of this class were initialized.

This led to undefined behavior (access to objects that are not initialized) that crashed into the application with this error at runtime.

How to solve the static problems of the initialization order can be found here .

+1
source

All Articles