C ++ fix std :: unordered_set SIGFPE
I wrote a program, and I am very surprised by the fact that I will run it dozens of times - I even recorded the results from several executions - and now it does not work.
You might think that I appreciate it as a snake to say this, or that I might just change some lines, but I really don't remember making any changes to the program.
The problem is SIGFPE
, which rises in different executions of the program, according to the input. It happens that the signal rises and inserts the value intostd::unordered_set<Point<T> *>
.
Here is a code snippet where I am doing an insert like this:
std::vector<Point<T> *> _point, _centroid;
std::vector<std::unordered_set<Point<T> *> > _cluster;
// Main procedure -- pseudocode
for (point in _point) {
cluster_id = centroid_with_min_distance(point, _centroid);
has_changed = _change_cluster(point, cluster_id);
}
// Changes from one "point->_cluster unordered_set" to "c unordered_set"
bool _change_cluster(Point<T> *point, const unsigned int& c) {
if ((point->_cluster == c) &&
(_cluster[c].find(point) != _cluster[c].end())) {
return false;
}
_cluster[point->_cluster].erase(point);
_cluster[c].insert(point); // Insertion that raises the SIGFPE exception
point->_cluster = c;
return true;
}
Here's an important section of valgrind's output:
==17636== Invalid read of size 8
==17636== at 0x40A758: std::pair<std::__detail::_Hashtable_iterator<
Point<unsigned int>*, true, false>, bool>
std::_Hashtable<Point<unsigned int>*, Point<unsigned int>*,
std::allocator<Point<unsigned int>*>,
std::_Identity<Point<unsigned int>*>,
std::equal_to<Point<unsigned int>*>,
std::hash<Point<unsigned int>*>,
std::__detail::_Mod_range_hashing,
std::__detail::_Default_ranged_hash,
std::__detail::_Prime_rehash_policy, false, true, true>::
_M_insert<Point<unsigned int>* const&>(
Point<unsigned int>* const&&&,
std::integral_constant<bool, true>) (hashtable.h:966)
==17636== by 0x408EDA: std::_Hashtable<Point<unsigned int>*,
Point<unsigned int>*, std::allocator<Point<unsigned int>*>,
std::_Identity<Point<unsigned int>*>,
std::equal_to<Point<unsigned int>*>,
std::hash<Point<unsigned int>*>,
std::__detail::_Mod_range_hashing,
std::__detail::_Default_ranged_hash,
std::__detail::_Prime_rehash_policy, false, true, true>::
insert(Point<unsigned int>* const&) (hashtable.h:400)
==17636== ... (calls from my program) ...
==17636== Address 0x620a028 is not stack'd, malloc'd or (recently) free'd
==17636==
==17636==
==17636== Process terminating with default action of signal 8 (SIGFPE)
==17636== Integer divide by zero at address 0x402D07D7C
==17636== at 0x40252F: std::__detail::_Mod_range_hashing::operator()(
unsigned long, unsigned long) const (hashtable_policy.h:376)
==17636== by 0x40A66C: std::__detail::_Hash_code_base<Point<unsigned int>*,
Point<unsigned int>*, std::_Identity<Point<unsigned int>*>,
std::equal_to<Point<unsigned int>*>,
std::hash<Point<unsigned int>*>,
std::__detail::_Mod_range_hashing,
std::__detail::_Default_ranged_hash, false>::_M_bucket_index(
Point<unsigned int>* const&, unsigned long,
unsigned long) const (hashtable_policy.h:758)
==17636== by 0x40A772: std::pair<std::__detail::_Hashtable_iterator<
Point<unsigned int>*, true, false>, bool>
std::_Hashtable<Point<unsigned int>*, Point<unsigned int>*,
std::allocator<Point<unsigned int>*>,
std::_Identity<Point<unsigned int>*>,
std::equal_to<Point<unsigned int>*>,
std::hash<Point<unsigned int>*>,
std::__detail::_Mod_range_hashing,
std::__detail::_Default_ranged_hash,
std::__detail::_Prime_rehash_policy, false, true, true>::
_M_insert<Point<unsigned int>* const&>(
Point<unsigned int>* const&&&,
std::integral_constant<bool, true>) (hashtable.h:966)
==17636== by 0x408EDA: std::_Hashtable<Point<unsigned int>*,
Point<unsigned int>*, std::allocator<Point<unsigned int>*>,
std::_Identity<Point<unsigned int>*>,
std::equal_to<Point<unsigned int>*>,
std::hash<Point<unsigned int>*>,
std::__detail::_Mod_range_hashing,
std::__detail::_Default_ranged_hash,
std::__detail::_Prime_rehash_policy, false, true, true>::
insert(Point<unsigned int>* const&) (hashtable.h:400)
==17636== ... (calls from my program) ...
The question is here: since there are calculations in my program that can lead to divisions by zero - they are not directly related to this process, although - is it possible that the error shaded by an insert? Or do I need to do some extra treatment by inserting pointers into std::unordered_set<T>
?
I am compiling a program for x86_64 GNU/Linux
and I am using g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
.
I suspect 'c' is out of range _cluster
. Can you replace everything _cluster[c]
with _cluster.at(c)
and see if it detects an out of range error.
The valgrind output indicates that this is an invalid read followed by an integer module error. Since it does hash_code % bucket_count
, I suspect you are out of range of the vector.
As otherwise, naming things that start with _
can be risky, as if it were in the global namespace, it is technically reserved for implementation.