Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3600404
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:29:27+00:00 2026-05-18T20:29:27+00:00

i have the following method : string Company::cheap(list<Candidate*>& candidates) { candidates.sort(candidateSalaryCompare); for (std::list<Candidate*>::iterator iter

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:29:27+00:00Added an answer on May 18, 2026 at 8:29 pm

    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:

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

    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:

    for (std::list<Candidate*>::iterator iter = candidates.begin(); 
         iter != candidates.end() && (*iter)->getExpectedSalary() == m; ++iter)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following method : string Company::cheap(list<Candidate*>& candidates) { candidates.sort(candidateSalaryCompare); for (std::list<Candidate*>::iterator iter
I have the following method : public List<string> someMethod() { // populate list of
I have the following method: public static string UlList(this HtmlHelper helper, List<IEntity> entities, string
I have the following bean method signatures: public String foo(); public List<String> getList(String bar);
I have the following method: public void PutFile(string ID, Stream content) { try {
Say I have the following line in a method: String encodedString = URLEncoder.encode(foo, utf-8);
I have the following method public static void SerializeToXMLFile(Object obj,Type type, string fileName) {
I have the following method: public object[] GetEventsByUser(DateTime start, DateTime end, string fullUrl) The
I have the following serialization method: Private Function SerializeData(ByVal data As cData) As String
I have the following class: public abstract class AbstractParent { static String method() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.