分段故障-接收到信号,SIGABORT



所以我的代码编译得很好,但每当我尝试运行程序时,它都会出错。我通过gdb运行它,得到了一组错误:

terminate called after throwing an 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) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56  ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.

如果有帮助的话,这是我的代码:

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 is local page, second entry is 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 i = 0; i < PagesNeeded; i++)
    {
      LocalPage = i;
      MainMemory.Pages[i].ProgramOwner = programNumber;
      MainMemory.Pages[i].LocalPageNumber = LocalPage; 
    }
     cout << "hey3";
    if(PagesNeeded > PagesPerProgram)
      PagesNeeded = PagesPerProgram;
    for(int i = 0; i < 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 does not exist." << endl;
      fileIn.close();
      exit(1);
    }
    else
    {
      fileIn >> activeProgram;
      fileIn >> requestedLocation;
      newPageLocation = ceil(requestedLocation/pageSize);
      while(!fileIn.eof())
      {
        foundInMemory = false;
        for(int i = 0; i < 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 i = 0; i < (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;
}

输出"heys"只是为了看看它们是否在gdb中被触发,而事实并非如此。

您实际上忘记问任何问题。

据推测,你的问题是"为什么我的程序会死于SIGABRT?"

答案是由程序本身提供的:您试图从NULL字符指针构造std::string,这会引发std::logic_error异常(因为这不是一个有效的操作)。

你可能会有一个后续问题:"在我的程序中,这种情况发生在哪里?"。

您可以使用GDBwhere命令找到答案。作为第一种猜测,您没有用足够数量的参数调用程序。例如,如果你这样做:

gdb ./a.out
(gdb) run

argv[1]NULL,并且该语句:

string programListFile = argv[1];

会抛出你得到的异常。

这个问题的常见解决方案是坚持你do有正确数量的参数,例如:

if (argc < 6) {
  std::cerr << "Not enough arguments" << std::endl;
  return 1;
}

在CCD_ 8的开始。或者只需在.

中注释对checkArguments()的调用

相关内容

  • 没有找到相关文章

最新更新