### URGENT: Segfault Problem

I'm writing a queue/scheduler program. The problem is after it prints out the high queue correctly, it segfaults. I've spent hours trying to fix this code and cant find a solution.

main
 ``1234567891011121314151617181920212223`` ``````Job j1("low", 25, "3rd"); Job j2("high", 100, "1st"); Job j3("high", 25, "2nd"); Job j4("low", 25, "4th"); Scheduler sch(0,50); sch.schedule(j1); sch.schedule(j2); sch.schedule(j3); sch.schedule(j4); sch.print_scheduler(cout); cout << endl <

scheduler.cpp
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116`` ``````template Scheduler::Scheduler(int time, int late){ //Scheduler constructor time_ = time; late_ = late; } template void Scheduler::schedule(T &job){ //Adds job to appropriate queue job.arrival_ = this->time_; // Sets the arrival time of the job if(job.priority_ == "low") { low_.push_back(job); } if(job.priority_ == "high") { high_.push_back(job); } } template ostream& Scheduler::print_scheduler(ostream& out){ //Prints scheduler out << "Scheduler, " << "Time:" << this->time_; out << ", Late:" << this->late_ << endl; out << "low queue" << endl; for(int i = 0; i < low_.size(); i++) //Prints low priority queue { out << " "; out << low_[i] << endl; } out << "high queue" << endl; for(int i = 0; i < high_.size(); i++) //Prints high priority queue { out << " "; out << high_[i] << endl; } return out; } template bool Scheduler::empty(){ if(high_.size() == 0 && low_.size() == 0) { return true; } else { return false; } } template T Scheduler::run(){ T selected; if(time_ - high_.front().arrival_ > late_) //High priority late, takes next high priority. { selected = high_.front(); time_ = time_ + selected.duration_; high_.pop_front(); //THIS WORKS FINE } else if(time_ - high_.front().arrival_ < late_ && time_ - low_.front().arrival_ >= late_) //Low priority late, takes next low priority. { selected = low_.front(); time_ = time_ + selected.duration_; low_.pop_front(); } else if(time_ - high_.front().arrival_ < late_ && time_ - low_.front().arrival_ < late_) //Neither late, takes next high priority. { selected = high_.front(); time_ = time_ + selected.duration_; high_.pop_front(); //THIS WORKS FINE } else if(high_.empty() == true) //High empty, picks next low priority. { selected = low_.front(); time_ = time_ + selected.duration_; low_.pop_front(); //I THINK ERROR IS IN THIS HIGH EMPTY STATEMENT SOMEHOW <------ } else if(low_.empty() == true) //Low empty, picks next high priority. { selected = high_.front(); time_ = time_ + selected.duration_; high_.pop_front(); } else if(high_.empty() == true && low_.empty() == true) { } return selected; } #endif /* SCHEDULER_H */ ``````

I have 2 deques, one low and one high priority. Currently the code correctly goes through and prints the jobs inside the high_ queue. When it gets past that point it dies with a segfault. How do I get it to stop dying at this point and go through with printing out the low queue? There is also a job.cpp and job.h but I didnt feel that it was necessary to post (yet?).

OUTPUT
 ``12345678910111213141516171819`` ``````Running Job, Name:1st Arrival:0 Duration:100 Scheduler, Time:100, Late:50 low queue Name:3rd Arrival:0 Duration:25 Name:4th Arrival:0 Duration:25 high queue Name:2nd Arrival:0 Duration:25 Running Job, Name:2nd Arrival:0 Duration:25 Scheduler, Time:125, Late:50 low queue Name:3rd Arrival:0 Duration:25 Name:4th Arrival:0 Duration:25 high queue RUN FINISHED; Segmentation fault;``````

Last edited on
Not having seen all of the code, my best guess is that after you finish running the high priority jobs you are calling high_.front() on a deque that is empty.

First line of code in run()
` if(time_ - high_.front().arrival_ > late_) // <--- What happens if high_ is empty? `

http://www.cplusplus.com/reference/deque/deque/front/
"Calling this function on an empty container causes undefined behavior."

Try checking if the the deques are empty() before you call front().

Will look into it.
Last edited on
 ``123`` ``````if(high_.size() == 0) { }``````

^ Added this as the first line of Run().

It doesnt segfault anymore but it still doesn't print the contents of the low_ queue, it just infinitely spits out "Running Job" with all the blank/default values.
Last edited on
Asked 2 other classmates, neither one said they have a "if the deque is empty" checker before other stuff in Run(). Guess that's not the issue :(
`run()` is always returning selected even if you fail all of you if checks which is what must be happenning.
Post your latest `run()` code.
Topic archived. No new replies allowed.