c ++中奇怪的分段错误

Mat*_*tan 2 c++

我有以下方法:

string Company::cheap(list<Candidate*>& candidates) {
    candidates.sort(candidateSalaryCompare);
    for (std::list<Candidate*>::iterator iter = candidates.begin(); iter
    != candidates.end(); ++iter) {
    }
    int m(candidates.front()->getExpectedSalary());
    list<Candidate*> potentialList;
    for (std::list<Candidate*>::iterator iter = candidates.begin(); (*iter)->getExpectedSalary()
    == m && iter != candidates.end(); ++iter)
        potentialList.push_back(*iter);
    if (potentialList.size() > 0)
        potentialList.sort(candidateIdCompare);
    return potentialList.front()->getId();
}
Run Code Online (Sandbox Code Playgroud)

按原样运行它,我的程序工作,但如果我在开头删除空FOR循环(没有做任何事情),我得到一个分段错误.任何线索?

编辑

候选类,实际上我不知道我在哪个行得到段错误,我正在使用eclipse并且调试器似乎不起作用

#include "../include/Candidate.h"
#include <iostream>
#include "../include/AppLogger.h"
#include <sstream>

Candidate::Candidate(string id, list<Skill> skills, list<
        string> desiredJobs, double expectedSalary) :
        id_(id), dateJoined_(), skills_(skills),
        desiredJobs_(desiredJobs), expectedSalary_(expectedSalary),
        originalSalary_(expectedSalary), gotJob_(0) {
}


void Candidate::compromise(const DateTime& currentDate) {
    double salaryAfter30(0.9*this->originalSalary_);
    double salaryAfter60(0.8*this->originalSalary_);
    double salaryAfter90(0.7*this->originalSalary_);
    Timespan duration = currentDate - this->dateJoined_;
    if (duration.days() == 30 || duration.days() == 60 || duration.days() == 90) {
        if (duration.days() == 30 && (this->expectedSalary_
                == this->originalSalary_)) {
            this->expectedSalary_ = salaryAfter30;
            std::stringstream sstm;
            sstm << "Candidate "<< this->getId() <<" is willing to compromise, and his expected salary is " <<this->expectedSalary_ << ".";
            CAppLogger::Instance().Log(sstm.str(),
                    Poco::Message::PRIO_WARNING);
            return;
        }
        else if (duration.days()==30)
            poco_bugcheck_msg("Error, 30 days passed, worker already compromised");
        if (duration.days() == 60 && (this->expectedSalary_ == salaryAfter30)) {
            this->expectedSalary_ = salaryAfter60;
            std::stringstream sstm;

            sstm << "Candidate "<< this->getId() <<" is willing to compromise, and his expected salary is " <<this->expectedSalary_ << ".";
            CAppLogger::Instance().Log(sstm.str(),
                    Poco::Message::PRIO_WARNING);
            return;
        }
        else if (duration.days()==60)
            poco_bugcheck_msg("Error, 60 days passed, worker already compromised");

        if ((duration.days() == 90) && (this->expectedSalary_ == salaryAfter60)) {
            this->expectedSalary_ = salaryAfter90;
            std::stringstream sstm;
            sstm << "Candidate "<< this->getId() <<" is willing to compromise, and his expected salary is " <<this->expectedSalary_ << ".";
            CAppLogger::Instance().Log(sstm.str(),
                    Poco::Message::PRIO_WARNING);
            return;
        }
        else if (duration.days()==90)
            poco_bugcheck_msg("Error, 90 days passed, worker already compromised");

    }
    else poco_bugcheck_msg("Error, worker told to compromise when not needed");


}

list<Skill> Candidate::getSkills() const {
    return this->skills_;
}

list<string> Candidate::getDesiredJobs() const {
    return this->desiredJobs_;

}
double Candidate::getExpectedSalary() const {
    return this->expectedSalary_;
}
DateTime Candidate::getDateJoined() const {
    return this->dateJoined_;
}
DateTime Candidate::getDateLeft() const {
    return this->dateLeft_;
}
void Candidate::setDateLeft(const DateTime& date) {
    this->dateLeft_ = date;
}
string Candidate::getId() const {
    return this->id_;
}

void Candidate::setDateJoined(const DateTime& date) {
    this->dateJoined_=date;
    this->setGotJob();
}

void Candidate::setGotJob() {
    if (this->gotJob_==1)
        std::cerr<<"error, setting gotJob while already has job"<<std::endl;
    this->gotJob_=1;
}
bool Candidate::gotJob() const {
    return this->gotJob_;
}
void Candidate::setQl(double ql){
        jobQl_=ql;
}

int Candidate::getQl() const{
    return this->jobQl_;
}
Run Code Online (Sandbox Code Playgroud)

应用提供的解决方案后,我收到以下错误:

assignment2(48823) malloc: *** mmap(size=140734799806464) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Run Code Online (Sandbox Code Playgroud)

编辑

将int m改为double m,现在它似乎有效,因为获得预期的薪水会返回一个双倍,但为什么会导致该错误?

Dav*_*eas 9

请看这里的 Cris Hopman答案,因为他的答案比我的答案更加完整和详细,指出了原始代码中可能/将导致未定义行为(分段错误)的其他问题.

这种情况是错误的:

for (std::list<Candidate*>::iterator iter = candidates.begin(); (*iter)->getExpectedSalary()
== m && iter != candidates.end(); ++iter)
Run Code Online (Sandbox Code Playgroud)

您应该重新排序,以便end()在取消引用之前进行测试并保持短路评估.实际上,您正在取消引用超过容器末尾的指针:

for (std::list<Candidate*>::iterator iter = candidates.begin(); 
     iter != candidates.end() && (*iter)->getExpectedSalary() == m; ++iter)
Run Code Online (Sandbox Code Playgroud)


Chr*_*man 7

让我稍微分解一下......

...
    int m(candidates.front()->getExpectedSalary());
Run Code Online (Sandbox Code Playgroud)

你认为候选人是非空的,如果记录在案,但是我会补充说这很好

assert(!candidates.empty())
Run Code Online (Sandbox Code Playgroud)

在功能的开头.这两者都强制执行约束,并使读取代码的任何人都明白约束.

其次,getExpectedSalary()返回一个你要转换为int的double ...稍后详细介绍.

    list<Candidate*> potentialList;
    for (std::list<Candidate*>::iterator iter = candidates.begin(); 
        (*iter)->getExpectedSalary() == m && iter != candidates.end(); ++iter)
       potentialList.push_back(*iter);
Run Code Online (Sandbox Code Playgroud)

这里有两个问题:iter != candidates.end()应该是条件的第一部分,否则你可能会在第一部分中取消引用无效的迭代器.其次,平等检查很糟糕.即使列表中的第一项也可能失败,因为如果d是双精度则不一定是这样(int)d == d.关键是m应该是双倍的.通过该更改,测试可能仍然意外失败,因为它是浮点比较,请参见此处.

    if (potentialList.size() > 0)
        potentialList.sort(candidateIdCompare);
    return potentialList.front()->getId();
Run Code Online (Sandbox Code Playgroud)

这是不一致的.在if您假设potentialList可能为空,但在返回中您假设它不能为空.实际上,potentialList不应该为空(因为它至少应该包含candidate.front()),所以这应该改为:

    assert(!potentialList.empty());
    potentialList.sort(candidateIdCompare);
    return potentialList.front()->getId();
Run Code Online (Sandbox Code Playgroud)

如果只做了那个改变,你会发现断言会失败.然后您将意识到potentialList是空的,因为上面讨论了从double到int的转换.

最后,您似乎将货币值存储为浮点数.这可能会引起问题,要小心.为此只使用定点通常更安全,但不是必需的.

最后一点,这个功能本身过于复杂.以下应该工作(没有保证)......

string Company::cheap(list<Candidate*>& candidates) {
    typedef list<Candidate*> list_t;
    assert(!candidates.empty());

    candidates.sort(candidateSalaryCompare);
    pair<list_t, list_t> rng = equal_range(candidates.begin(), candidates.end(),
        candidates.front(), candidateSalaryCompare);

    assert(rng.first != rng.second);
    return (*min_element(rng.first, rng.second, candidateIdCompare))->getId();
}
Run Code Online (Sandbox Code Playgroud)

这也方便地将所有工资比较卸载到另一个功能,因此只需要正确完成一次.

  • +1,这个答案比我的答案更加完整和详细,值得被接受,并获得更多的声誉. (2认同)