0

我正在尝试实现一个最近最少使用的算法程序。我正在提示用户输入帧大小。如果我提示并输入 6 下的任何内容,它会完美运行。如果我输入 6 及以上的任何内容,它会在读取输入流(文件)并将其添加到类时引发异常。

inputStream >> pid;
inputStream >> ref;

文件有 7-8 行,我这里只显示 2 行。

1 45
1 46

这是我的课程和 main() 的一部分

class pagetable
{

public:
int pid;
int ref;
int faults;
pagetable();
};

 pagetable::pagetable(){

pid = 0;
ref = 0;
faults = 0;
}

主要的()

while(!done){
 pagetable* page = new pagetable[frames];
ifstream inputStream;
getFileName(inputStream);//asks for input filename until it is valid

cout << "\nEnter in the number of frames:";
cin >> frames;
for ( i = 0; i < frames; i++ ) { //initializing
     page[i].pid = 0;
     page[i].ref = 0;


}
faults = runsimLFU2(inputStream, page, frames );

  void getFileName(ifstream &inputStream) //asks for input file until it is valid
{
char filename[MAXFILE];
while (inputStream.is_open() == false)
{
    inputStream.clear();
    cout << "\n";
    cout << "Input filename: ";
    cin >> filename;
    inputStream.open(filename);
}
} 

所以,现在我调用一个运行 LRU 算法的函数。当我将文件解析到类时,这就是我得到错误的地方。我评论了我收到错误的那一行。

int runsimLFU2(ifstream &inputStream, pagetable* page, int frames ){

int i =0;
int j=0;
int pid =0;
int ref = 0;
int index = 0;
int count = 0;
int pagefaults = 0;
int lowest=0;

int counter = 1;

int * LRU;
LRU = new int[frames];


while(1){

  inputStream >> pid;        //Error if frame is 6 or more
  inputStream >> ref;
  if( inputStream.eof() ) break;
    while(count < frames)
   {

       index  = searchForEmptySlotsLRU(page, frames);

它在哪里引发错误 VS 调出 xlocale 文件,我评论了哪一行

_CRTIMP2_PURE void __CLR_OR_THIS_CALL _Incref()
        {   // safely increment the reference count
        _BEGIN_LOCK(_LOCK_LOCALE)
            if (_Refs < (size_t)(-1))   //error
                ++_Refs;
        _END_LOCK()
        }

这可能是我初始化的方式吗?我需要将它们初始化为零,因为稍后我会检查空槽。

我真的不明白,因为那时我没有在课堂上添加任何东西。

谢谢你。

编辑:我注释掉了类中的初始化并且它不再抛出异常。

4

1 回答 1

0

你以某种方式破坏了记忆。

这看起来很可疑:

while(!done){
    pagetable* page = new pagetable[frames];
    ifstream inputStream;
    getFileName(inputStream);//asks for input filename until it is valid

    cout << "\nEnter in the number of frames:";
    cin >> frames;
    for ( i = 0; i < frames; i++ ) { //initializing
        page[i].pid = 0;
        page[i].ref = 0;    
    }

第一次帧的价值是多少?尝试将其移至cin.

while(!done){
    ifstream inputStream;
    getFileName(inputStream);//asks for input filename until it is valid

    cout << "\nEnter in the number of frames:";
    cin >> frames;
    pagetable* page = new pagetable[frames];
    for ( i = 0; i < frames; i++ ) { //initializing
于 2012-12-01T23:54:14.303 回答