i have the following method :
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();
}
running it as is and my program works, but if i remove the empty FOR loop in the beginning (which doesn’t do anything) i get a segmentation fault.
any clues ?
EDIT
candidate class, and actually i’m not sure at what line i’m getting the segfault,
i’m using eclipse and the debugger doesn’t seem to work
#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_;
}
after applying the supplied solutions i get the following error :
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
EDIT
changed int m to double m and now it seems to work
because get expected salary returns a double
but why did it cause that error ?
Please, look at Cris Hopman answer here, as his answer is much more complete and detailed than mine pointing to other issues that can/will cause undefined behavior (segmentation fault) in the original code.
This condition is wrong:
You should reorder so that the test for
end()comes before the dereference and keep the short circuit evaluation. As it is, you are dereferencing a pointer past the end of the container: