Stepan Yakovenko Stepan Yakovenko - 11 months ago 77
C++ Question

Crash in C++ std::map

I think I have isolated crash which happens only on cygwin platform. Tried to run with valgrind on linux — no effect, it reports no memory errors. I have function to remove zero monomes from Polynomes

typedef map<multiset<int>, double> Polynome;

void removeZeroes(Polynome&p){
for(auto it=p.cbegin();it!=p.cend();){
if(p[it->first]==0.) {
} else ++it;

void calcGDerivatives(Reduced &r) {
auto vars = getAllVars(r);
cout<<"calculating derivatives"<<endl;
iterate(vars, [&r](multiset<int>k1)->void {
if (r.grandDerivatives.count(k1)) return;
Polynome der = r.grandDerivatives[k1];
for (auto &kv : r.grandPoly) {
Monome monDer = monomeDeriv(kv.first, k1);
multiset<int> p = kv.first;
if (monDer.first == 0) continue;
monDer.first *= kv.second;
add(der, monDer);
r.grandDerivatives[k1]=der; //since der is a copy

If I replace Polynome der to reference &der and remove[k1]=der, then I get a crash. Is it unsafe to modify std::map's value? How can I find a place were problem happens? Thanx!

Answer Source

I've finally found true cause of the problem. One of my functions looked like this:

stl::vector<...> f(){
   // do something
   // forgot return

This immediately gives a crash on cygwin.