1、可变分区存储管理方式的内存分配和回收精品文档#include/定义输入/输出函数#include/数据流输入/输出 #include/字符串处理#include/参数化输入/输出const int MJ=10;/假定系统允许的最大作业数量为10typedef struct nodeint address;int length;char tag10;job;job freesMJ;int free_quantity;job occupysMJ;int occupy_quantity;int read()FILE *fp;char fn10;coutfn;if(fp=fopen(fn,r)=NUL
2、L) 其意义是在当前目录下打开文件file a,只允许进行“读”操作,并使fp指向该文件cout错误,文件打不开,请检查文件名endl;elsewhile(!feof(fp)fscanf(fp,%d,%d,&freesfree_quantity.address,&freesfree_quantity.length);free_quantity+; fscanf(文件指针,格式字符串,输入表列);return 1;return 0;void sort()int i,j,p;for(i=0;ifree_quantity-1;i+)p=i;for(j=i+1;jfree_quantity;j+)if
3、(freesj.addressfreesp.address)p=j;if(p!=i)freesfree_quantity=freesi;freesi=freesp;freesp=freesfree_quantity;void view()int i;coutendlmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmendl;cout输出空闲区表:n起始地址 分区长度 状态nendl;for(i=0;ifree_quantity;i+)cout.setf(2);cout.width(12);coutfreesi.address;
4、cout.width(10);coutfreesi.length;cout.width(8);coutfreesi.tagendl;coutendlwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwendl;cout输出已分分区表:n起始地址 分区长度 占用作业名nendl;for(i=0;ioccupy_quantity;i+)cout.setf(2);cout.width(12);coutoccupysi.address;cout.width(10);coutoccupysi.length;cout.width(8);c
5、outoccupysi.tagendl;void ear()char job_name10;int job_length;int i,j,flag,t;coutjob_name;cinjob_length;flag=0;for(i=0;i=job_length)flag=1;if(flag=0) /未找到空闲区,返回coutendl无可用的空闲区n=job_length)/找到可用空闲区,开始分配t=1;i+;i-;occupysoccupy_quantity.address=freesi.address; /修改已分配区表 strcpy(occupysoccupy_quantity.tag,
6、job_name);occupysoccupy_quantity.length=job_length;occupy_quantity+;if(freesi.lengthjob_length)freesi.address+=job_length;freesi.length-=job_length;elsefor(j=i;jfree_quantity-1;j+)freesj=freesj+1;free_quantity-;cout内存空间成功:)endl; void reclaim()/回收作业所占的内存空间char job_name20;int i,j,flag,p=0;int address;
7、int length;/寻找已分分区表中对应的登记项coutjob_name;flag=-1;for(i=0;ioccupy_quantity;i+)if(!strcmp(occupysi.tag,job_name)flag=i;address=occupysi.address;length=occupysi.length;if(flag=-1) /在已分分区表中找不到作业cout没有这个作业名endl;else/修改空闲区表,加入空闲表for(i=0;ifree_quantity;i+)if(freesi.address+freesi.length)=address)if(i+1)free_
8、quantity)&(freesi+1.address=address+length)freesi.length=freesi.length+freesi+1.length+length;for(j=i+1;jfree_quantity;j+)freesj=freesj+1;free_quantity-;p=1;elsefreesi.length+=length;p=1;if(freesi.address=(address+length)freesi.address=address;freesi.length+=length;p=1;if(p=0)freesfree_quantity.addr
9、ess=address;freesfree_quantity.length=length;free_quantity+;/删除分配表中的该作业for(i=flag;ioccupy_quantity;i+)occupysi=occupysi+1;occupy_quantity-;void main()int flag=0;int t=1;int chioce=0;int i;for(i=0;iMJ;i+)freesi.address=-1;/空闲区表初始化freesi.length=0;strcpy(freesi.tag,free);occupysi.address=-1;/已分分区表初始化oc
10、cupysi.length=0;strcpy(occupysi.tag,);free_quantity=0;occupy_quantity=0;flag=read();while(flag=1)sort();cout选择功能项: (0-退出,1-分配内存,2-回收内存,3-显示内存)nendl;coutchioce;switch(chioce)case 0: flag=0;break;case 1:ear(); break; case 2:reclaim();break;case 3:view();break;default:cout没有该选项nendl;收集于网络,如有侵权请联系管理员删除
©2010-2024 宁波自信网络信息技术有限公司 版权所有
客服电话:4008-655-100 投诉/维权电话:4009-655-100