This publication is for those who are obliged on duty to use the pgcpp compiler or maintain code compatibility with this compiler.
The other day I received a bug report that my code does not work correctly if compiled with pgcpp.
Starting to understand, I found a place where the error occurs. It turned out that if the code is compiled with O2 or O3 optimization, then std :: sort can start duplicating part of the vector and replacing other parts with these duplicates.
')
Here is a simple C ++ code to help recreate this horrible behavior (note the number 3193 in the output):
#include <iostream> #include <algorithm> #include <vector> struct ID{ ID(){}; ID(unsigned id) : Id(id){}; unsigned Id; bool operator < ( ID const &other ) const { return Id < other.Id; } }; main(){ std::vector<ID> ids; for (unsigned i=0; i < 5; ++i) for (unsigned id=0; id < 2000; ++id ) ids.push_back(ID(id*5+i)); std::sort(ids.begin(),ids.end()); // std::stable_sort(ids.begin(),ids.end()); for (std::vector<ID>::const_iterator it=ids.begin();it!=ids.end();++it) std::cout << it->Id << std::endl; }
I tested on Linux, but presumably the error happens on Windows.
I wrote in support and they replied that they were able to recreate "this behavior", so there is hope that the bug will be fixed soon. In the meantime, I recommend using either std :: stable_sort or (if you can choose) another compiler (the Portland Group also has an alternative compiler - pgc ++ - and there is no bug there).
All good debugging and fewer bugs in compilers.