通信实验 动态分区存储管理方式的主存分配回收 查错

此次查错结论:对于单链表节点删除要考虑 :该节点是 表头,表尾,还是表间的节点 。

对于一个指针不对其申请内存,该指针是空的。对空指针去进行操作会导致程序崩溃的。

#include<iostream>
#include<algorithm>
#include <iomanip>
#include<cstdio>
using namespace std;


#define ERR_NOFREEAREA 1
#define ERR_NOADEQUACYAREA 2
#define ERR_ALLOCATED 4
#define ERR_NOJOBS 1
#define ERR_NOSUCHJOB 2
#define ERR_RECLAIMED 4

typedef struct tagUsedNode{
 long address;
 long length;
 int flag;
 struct tagUsedNode *next;
} USED_AREA , *USED_TABLE;

typedef struct tagFreeNode{
 long address;
 long length;
 struct tagFreeNode *next;
} FREE_AREA , *FREE_TABLE;

USED_TABLE usedTable = NULL;
FREE_TABLE freeTable = NULL;


void Init(){
     freeTable = new FREE_AREA();
     freeTable->address = 0;
     freeTable->length = 1024;
     freeTable->next = NULL;

//     usedTable =new USED_AREA();
//     usedTable->address = -1;
//     usedTable->length = 0;
//     useTable->next = NULl;
}

int Allocate( int jobname , long jobsize ){

     if( freeTable == NULL )   return ERR_NOFREEAREA;

     FREE_TABLE p = freeTable;
     FREE_TABLE q = p;

     while( p != NULL && p->length < jobsize ){
          q = p;
          p = p->next;
     }

      if( p == NULL )   return ERR_NOADEQUACYAREA;


     USED_TABLE x = new USED_AREA;
     x->address = p->address;
     x->length = jobsize;
     x->flag = jobname;
     x->next = NULL;


     if( p->length > jobsize ){                     // 修改空闲列表
          p->length -= jobsize;
          p->address += jobsize;
     }

     else {
      if( p == freeTable )   freeTable = NULL;
        else   q->next = p->next;
        delete p;
     }


     USED_TABLE r = usedTable;
     USED_TABLE t = r;

     while( r != NULL && r->address < x->address ){
          t = r;
          r = r->next;
     }

     if( usedTable == NULL )    usedTable = x;

     else {
            if( r == NULL ){
                t->next = x;
            }
            else if( t == r ){
                x->next = usedTable ;
                usedTable= x;
            }
            else{
                  x->next = r;
                  t->next = x;
            }
     }
     return ERR_ALLOCATED;
}

void jobrequest(){
     int jobname;
     int jobsize;
     cout<<"...................."<<endl;
     cout<<"作业名: ";
     cin >> jobname;
     cout<<"作业长度: ";
     cin >> jobsize;
     if( Allocate( jobname , jobsize ) == ERR_ALLOCATED )      cout<<"该作业已成功获得所需空间"<<endl;

     else     cout<<"该作业没有获得所需空间"<<endl;

     cout<<"...................."<<endl;
}

int Reclaim( int jobname ){

     if( usedTable == NULL )   return ERR_NOJOBS;

     USED_TABLE p = usedTable;
     USED_TABLE q = p;
     while( p != NULL && p->flag != jobname ){
          q = p;
          p = p->next;
     }

     if( p == NULL )   return ERR_NOSUCHJOB;


     FREE_TABLE r = freeTable;
     FREE_TABLE t = r;

     FREE_TABLE x;

     while( r != NULL && r->address < p->address ){
          t = r;
          r = r->next;
     }

     x = new FREE_AREA;                // 必须提前 
     x->address = p->address;
     x->length = p->length;
     x->next = NULL;

     if( r == freeTable ){
          x->next = r;
          freeTable = x;
         // t = freeTable;
     }

     else if( r == NULL){
        t->next = x;
     }

     else {
          x->next = r;
          t->next = x;
     }

     while( t->next != NULL && t->address + t->length == t->next->address ){
          t->length += t->next->length;
          r = t->next;
          t->next = t->next->next;
          delete r;
     }

     if( p == usedTable )  usedTable = usedTable->next;
     else    q->next = p->next;
     delete p;
     return ERR_RECLAIMED;
}

void jobreclaim(){
     int jobname;
     cout<<"...................."<<endl;
     cout<<"作业名: ";
     cin >>jobname;
     int result = Reclaim( jobname );
     if(  result == ERR_RECLAIMED )    cout<<"该作业已成功回收"<<endl;

     else if( result == ERR_NOSUCHJOB || result == ERR_NOJOBS )    cout<<"系统没有作业或该作业不存在"<<endl;
     cout<<"...................."<<endl;
}

void freeTablePrint(){
     cout<<"........................................"<<endl;
     cout<<setw(10)<<"address"<<setw(10)<<"length"<<setw(10)<<"state"<<endl<<endl;
     FREE_TABLE p = freeTable;
     USED_TABLE q = usedTable;
     int x , y;
    while( p || q ){
          if( p )   x = p->address;
          else      x = 0x7fffffff;

          if( q )   y = q->address;
          else      y = 0x7fffffff;

          if( x < y ){
               cout<<setw(10)<<p->address<<setw(10)<<p->length<<setw(10)<<"空闲"<<endl;
               p = p->next;
          }
          if(y < x){
               cout<<setw(10)<<q->address<<setw(10)<<q->length<<setw(10)<<"已分配"<<setw(10)<<"ID="<<q->flag<<endl;
               q = q->next;
          }
     }
     cout<<"........................................"<<endl;
}

int main(){
     Init();
     int choose;
     bool exitFlag = false;
     while( !exitFlag ){
          cout<<"选择功能项 ( 0 - 退出   1 - 分配主存  2 - 回收主存  3 - 显示主存 )"<<endl;
          cout<<"?>";
          cin>>choose;
          switch( choose ){
              case 0:exitFlag = true; break;
              case 1:jobrequest();break;
              case 2:jobreclaim();break;
              case 3:freeTablePrint();break;
          }
     }
     return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值