c++ - Seg Fault - Signal received, SIGABORT -
so code compiles well, whenever try running program, seg faults. ran through gdb , got set of error:
terminate called after throwing instance of 'std::logic_error' what(): basic_string::_s_construct null not valid program received signal sigabrt, aborted. 0x00007ffff722bcc9 in __gi_raise (sig=sig@entry=6) @ ../nptl/sysdeps/unix/sysv/linux/raise.c:56 56 ../nptl/sysdeps/unix/sysv/linux/raise.c: no such file or directory.
here code if helps:
using namespace std; #include <iostream> #include <vector> #include <cmath> #include<fstream> #include <cstdlib> #include <utility> #include <string> class page { public: int timestamp; int programowner; int localpagenumber; }; //pair: first entry local page, second entry global page //second entry: 1 if in main memory, 0 if not class program { public: vector< pair<int,int> > memorymap; }; class memory { public: vector<program> programs; vector<page> pages; }; void checkarguments(int argc, char *argv[]); bool checkpagealgorithm(char *argv[]); bool checkpagesize(int pagesize); bool checkpagestyle(char *argv[]); bool checkptrace(string programtracefile); bool checkplist(string programlistfile); int main(int argc, char *argv[]) { //checkarguments(argc, argv); const int memsize = 512; int pagesize = atoi(argv[3]); string programlistfile = argv[1]; string programtracefile = argv[2]; string pagealgorithm = argv[4]; string pagestyle = argv[5]; int programnumber; int programsize; int pagesneeded; memory mainmemory; int numberframes; int pagefaults = 0; int numprograms = 0; int pagesperprogram; int localpage; int activeprogram; int requestedlocation; int newpagelocation; bool foundinmemory; ifstream filein; filein.open(programlistfile.c_str()); if(filein.fail()) { cerr << "list file not found." << endl; filein.close(); exit(1); } else { while(!filein.eof()) { cout << "hey0"; filein >> programnumber; filein >> programsize; numprograms++; } } numberframes = ceil(memsize/pagesize); pagesperprogram = ceil(numberframes/numprograms); mainmemory.pages.resize(numberframes); cout << "hey1"; while(!filein.eof()) { cout << "hey2"; filein >> programnumber; filein >> programsize; cout << "hey3"; pagesneeded = ceil(programsize/pagesize); for(int = 0; < pagesneeded; i++) { localpage = i; mainmemory.pages[i].programowner = programnumber; mainmemory.pages[i].localpagenumber = localpage; } cout << "hey3"; if(pagesneeded > pagesperprogram) pagesneeded = pagesperprogram; for(int = 0; < pagesneeded; i++) { mainmemory.programs[programnumber].memorymap[i].first = mainmemory.pages[i].localpagenumber; mainmemory.programs[programnumber].memorymap[i].second = 1; if(pagealgorithm == "clock") mainmemory.pages[i].timestamp = 1; else mainmemory.pages[i].timestamp = 0; } } filein.close(); //lru algorithm implementation if(pagealgorithm == "lru") { cout << "here1"; filein.open(programtracefile.c_str()); if(filein.fail()) { cerr << "that file not exist." << endl; filein.close(); exit(1); } else { filein >> activeprogram; filein >> requestedlocation; newpagelocation = ceil(requestedlocation/pagesize); while(!filein.eof()) { foundinmemory = false; for(int = 0; < static_cast<int>(mainmemory.programs[activeprogram].memorymap.size()); i++) { if((mainmemory.programs[activeprogram].memorymap[i].second == 1) && (mainmemory.programs[activeprogram].memorymap[i].first == newpagelocation)) foundinmemory = true; if(foundinmemory) break; } if(!foundinmemory) { pagefaults++; if(static_cast<int>(mainmemory.programs[activeprogram].memorymap.size()) < pagesperprogram) { pair<int, int> temp; temp.first = newpagelocation; temp.second = 1; mainmemory.programs[activeprogram].memorymap.push_back(temp); } else { for(int = 0; < (static_cast<int>(mainmemory.programs[activeprogram].memorymap.size()) - 1); i++) { if(mainmemory.pages[i].timestamp >= mainmemory.pages[i+1].timestamp) { mainmemory.programs[activeprogram].memorymap[i].first = newpagelocation; } if(pagestyle == "1") { if(mainmemory.pages[i].timestamp >= mainmemory.pages[i+1].timestamp) { mainmemory.programs[activeprogram].memorymap[i].first = mainmemory.pages[i].localpagenumber; } } mainmemory.pages[i].timestamp++; } } } filein >> activeprogram; filein >> requestedlocation; newpagelocation = ceil(requestedlocation/pagesize); } } } cout << "------------------------------------" << endl; cout << "page size: " << pagesize << endl; cout << "page replacement algorithm: " << pagealgorithm << endl; cout << "paging style: "; if(pagestyle == "0") cout << "demand" << endl; else cout << "prepaging" << endl; cout << "number of page faults: " << pagefaults << endl; cout << "------------------------------------" << endl; return 0; } bool checkplist(string programlistfile) { ifstream listfile(programlistfile.c_str()); if(listfile.fail()) { cerr << "cannot find file " << programlistfile << endl; listfile.close(); return false; } else { listfile.close(); return true; } } bool checkptrace(string programtracefile) { ifstream tracefile; tracefile.open(programtracefile.c_str()); if(tracefile.fail()) { cerr << "cannot find file " << programtracefile << endl; tracefile.close(); return false; } else { tracefile.close(); return true; } } bool checkpagestyle(string pagestyle) { if(pagestyle == "0") return true; else if(pagestyle == "1") return true; else { cerr << "page style can be: 0 or 1." << endl; return false; } } bool checkpagesize(int pagesize) { bool isvalid = false; switch(pagesize) { case 1: isvalid = true; break; case 2: isvalid = true; break; case 4: isvalid = true; break; case 8: isvalid = true; break; case 16: isvalid = true; break; default: cerr << "page size can be: 1, 2, 4, 8, or 16." << endl; isvalid = false; } return isvalid; } bool checkpagealgorithm(string pagealgorithm) { if(pagealgorithm == "lru") return true; else if(pagealgorithm == "fifo") return true; else if(pagealgorithm == "clock") return true; else { cerr << "valid page algorithms are: lru, fifo, or clock" << endl; return false; } } void checkarguments(int argc, char *argv[]) { if(argc < 6) { cerr << "invalid number of arguments. should be: programlist programtrace pagesize pagealgorithm pagingstyle" << endl; exit(1); } else if(argc > 6) { cerr << "invalid number of arguments. should be: programlist programtrace pagesize pagealgorithm pagingstyle" << endl; exit(1); } else if(!checkpagealgorithm(argv[4])) exit(1); else if(!checkpagesize(atoi(argv[3]))) exit(1); else if(!checkpagestyle(argv[5])) exit(1); else if(!checkptrace(argv[2])) exit(1); else if(!checkplist(argv[1])) exit(1); return; }
the output 'heys' see if triggered within gdb, don't.
you forgot ask question.
presumably, question "why program die sigabrt
?"
the answer provided program itself: you've tried construct std::string
null
character pointer, throws std::logic_error
exception (because it's not valid thing do).
you may have follow-up quesiton: "where in program happen?".
you find answer using gdb where
command. first guess, didn't invoke program sufficient number of arguments. example, if did this:
gdb ./a.out (gdb) run
then argv[1]
null
, , statement:
string programlistfile = argv[1];
would throw exception getting.
the common solution problem insist do have correct number of arguments, e.g. put this:
if (argc < 6) { std::cerr << "not enough arguments" << std::endl; return 1; }
at start of main
. or comment call checkarguments()
in.
Comments
Post a Comment