c ++中奇怪的分段错误

时间:2010-12-13 15:53:06

标签: 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();
}

按原样运行并且我的程序正常工作,但是如果我在开头删除空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_;
}

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

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

修改

将int m更改为double m,现在似乎可以正常工作 因为得到的预期薪水会增加一倍 但为什么会导致这个错误?

3 个答案:

答案 0 :(得分:9)

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

这种情况是错误的:

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

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

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

答案 1 :(得分:7)

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

...
    int m(candidates.front()->getExpectedSalary());

你认为候选人是非空的,如果记录在案,但我会添加

,这很好
assert(!candidates.empty())

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

其次,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);

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

    if (potentialList.size() > 0)
        potentialList.sort(candidateIdCompare);
    return potentialList.front()->getId();

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

    assert(!potentialList.empty());
    potentialList.sort(candidateIdCompare);
    return potentialList.front()->getId();

如果您只进行了更改,您会发现断言将失败。然后,您将意识到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();
}

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

答案 2 :(得分:2)

你的第二个循环,特别是条件,可能是危险的:

(*iter)->getExpectedSalary() == m && iter != candidates.end()

这是因为在实际检查迭代器是否有效之前,它会将iterm 进行比较。您需要反转检查的顺序,以便条件变为:

iter != candidates.end() && (*iter)->getExpectedSalary() == m