weird segmentation fault in c++
<p>i have the following method :</p>

<pre><code>string Company::cheap(list&lt;Candidate*&gt;&amp; candidates) {
    candidates.sort(candidateSalaryCompare);
    for (std::list&lt;Candidate*&gt;::iterator iter = candidates.begin(); iter
    != candidates.end(); ++iter) {
    }
    int m(candidates.front()-&gt;getExpectedSalary());
    list&lt;Candidate*&gt; potentialList;
    for (std::list&lt;Candidate*&gt;::iterator iter = candidates.begin(); (*iter)-&gt;getExpectedSalary()
    == m &amp;&amp; iter != candidates.end(); ++iter)
        potentialList.push_back(*iter);
    if (potentialList.size() &gt; 0)
        potentialList.sort(candidateIdCompare);
    return potentialList.front()-&gt;getId();
}
</code></pre>

<p>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 ?</p>

<p>EDIT</p>

<p>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</p>

<pre><code>#include "../include/Candidate.h"
#include &lt;iostream&gt;
#include "../include/AppLogger.h"
#include &lt;sstream&gt;

Candidate::Candidate(string id, list&lt;Skill&gt; skills, list&lt;
        string&gt; desiredJobs, double expectedSalary) :
        id_(id), dateJoined_(), skills_(skills),
        desiredJobs_(desiredJobs), expectedSalary_(expectedSalary),
        originalSalary_(expectedSalary), gotJob_(0) {
}


void Candidate::compromise(const DateTime&amp; currentDate) {
    double salaryAfter30(0.9*this-&gt;originalSalary_);
    double salaryAfter60(0.8*this-&gt;originalSalary_);
    double salaryAfter90(0.7*this-&gt;originalSalary_);
    Timespan duration = currentDate - this-&gt;dateJoined_;
    if (duration.days() == 30 || duration.days() == 60 || duration.days() == 90) {
        if (duration.days() == 30 &amp;&amp; (this-&gt;expectedSalary_
                == this-&gt;originalSalary_)) {
            this-&gt;expectedSalary_ = salaryAfter30;
            std::stringstream sstm;
            sstm &lt;&lt; "Candidate "&lt;&lt; this-&gt;getId() &lt;&lt;" is willing to compromise, and his expected salary is " &lt;&lt;this-&gt;expectedSalary_ &lt;&lt; ".";
            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 &amp;&amp; (this-&gt;expectedSalary_ == salaryAfter30)) {
            this-&gt;expectedSalary_ = salaryAfter60;
            std::stringstream sstm;

            sstm &lt;&lt; "Candidate "&lt;&lt; this-&gt;getId() &lt;&lt;" is willing to compromise, and his expected salary is " &lt;&lt;this-&gt;expectedSalary_ &lt;&lt; ".";
            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) &amp;&amp; (this-&gt;expectedSalary_ == salaryAfter60)) {
            this-&gt;expectedSalary_ = salaryAfter90;
            std::stringstream sstm;
            sstm &lt;&lt; "Candidate "&lt;&lt; this-&gt;getId() &lt;&lt;" is willing to compromise, and his expected salary is " &lt;&lt;this-&gt;expectedSalary_ &lt;&lt; ".";
            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&lt;Skill&gt; Candidate::getSkills() const {
    return this-&gt;skills_;
}

list&lt;string&gt; Candidate::getDesiredJobs() const {
    return this-&gt;desiredJobs_;

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

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

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

int Candidate::getQl() const{
    return this-&gt;jobQl_;
}
</code></pre>

<p>after applying the supplied solutions i get the following error :</p>

<pre><code>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
</code></pre>

<p>EDIT</p>

<p>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 ?</p>
