Home > Net >  C Error (Segmentation fault / Bus error / Memory limit exceeded / Stack limit exceeded) when using
C Error (Segmentation fault / Bus error / Memory limit exceeded / Stack limit exceeded) when using

Time:04-04

My program crashes after uploading to the school test server with the announcement that one of these errors has occurred (Segmentation fault / Bus error / Memory limit exceeded / Stack limit exceeded), I have no exact information. If I run the program in the debugger, I can't find anything. The audit method may fail.

The whole program https://onecompiler.com/cpp/3xy2j7dmd

class Company
{
    private:
        string name;
        string addr;
        string id;
        unsigned int totalIncome;
        unsigned int numberOrders;
        unsigned int amount;
};
bool Company::cmpNA (const Company &a, const Company &b) 
{
    if ( a.getName() < b.getName())
        return a.getName() < b.getName();
    
    return a.getAddr() < b.getAddr();
}
bool CVATRegister::audit ( const string &name, const string  &addr, unsigned int &sumIncome ) const
{
    Company cmp(name, addr,"-1");
    vector<Company> tmp = DCompany;

    sort(tmp.begin(), tmp.end(), [](const Company & a, const Company & b)
        { 
            if ( a.getName() < b.getName())
                return a.getName() < b.getName();
    
             return a.getAddr() < b.getAddr();
        });

    auto itr = lower_bound(tmp.begin(), tmp.end(), cmp, &Company::cmpNA);

    if(itr != tmp.end() && addr == itr->getAddr() && itr->getName() == name)
     {   
        sumIncome = itr->getTotalIncome(); 
        return true;
     }
     return false;
}

CodePudding user response:

That sort lambda doesn't provide strict weak ordering as required by std::sort. Both sort and lower_bound can/will fail.

Did you mean?

  sort(tmp.begin(), tmp.end(), [](const Company & a, const Company & b)
    { 
        if ( a.getName() != b.getName())
            return a.getName() < b.getName();

         return a.getAddr() < b.getAddr();
    });
  • Related