问题遇到的现象和发生背景
在运行代码时,showArea函数怎么也不运行
问题相关代码,请勿粘贴截图
#include<time.h>
#include <stdio.h>
#include <stdlib.h>
#define SIZE 512
#define MINSIZE 5
struct memory
{
struct memory *former;//前向指针
int address;//地址
int num;//作业号
int size;//分配内存大小
int state;//状态0表示空闲,1表示已分配
struct memory *next;//后向指针
}LinkList;
```c
```c
void init()
{
memory *p=(memory *)malloc(sizeof(memory));
p->address=0;
p->size=SIZE;
p->state=0;
p->num=-1;
p->former=&LinkList;//初始时没有作业调入内存
p->next=NULL;
LinkList.next=p;
}
int FF(int num,int size)//首次适应算法
{
memory *p=LinkList.next;
while(p!=NULL)
{
if((p->state==0)&&(p->size>=size))
{
if(p->size<=MINSIZE)
{
p->state=1;
p->num=num;
}
else//分配大小为size的空闲分区
{
memory *node=(memory*)malloc(sizeof(memory));
node->address=p->address+size;
node->size=p->size-size;
node->state=0;
node->num=-1;
node->former=p;
node->next=p->next;
if(p->next!=NULL)
{
p->next->former=node;
}
p->next=node;//分配空闲区间
p->size=size;
p->state=1;
p->num=num;
}
printf("内存分配成功!\n");
return 1;
}
p=p->next;
}
printf("找不到合适的内存分区,分配失败\n");
return 0;
}
```c
int BF(int num,int size)
{
memory *q=NULL;
int tarsize=SIZE+1;
memory *p=LinkList.next;
while(p!=NULL)
{
if((p->state=0)&&((p->size>=size)&&(p->size<=tarsize)))//寻找最佳空闲区间
{
q=p;
p->size=tarsize;
}
p=p->next;
}
if(q!=NULL)
{
if(q->size-size<=MINSIZE)//直接整块分配
{
q->state=1;
q->num=num;
}
else
{
memory *lnode=(memory *)(sizeof(memory));
lnode->address=q->address+size;
lnode->size=q->size-size;
lnode->state=0;
lnode->num=-1;
lnode->former=q;
lnode->next=q->next;
if(q->next!=NULL)
{
q->next->former=lnode;
}
q->next=lnode;
q->size=size;
q->state=1;
q->num=num;
printf("内存分配成功!\n");
}
return 1;
}
else
{
printf("内存分配失败!\n");
return 0;
}
}
int release(int num)
{
int flag=0;
memory *p=LinkList.next,*pp;
while(p!=NULL)
{
if((p->state==1)&&(p->num==num))
{
flag=1;
if((p->former!=&LinkList)&&(p->former->state==0)&&(p->next!=NULL)&&(p->next->state==0))//上下均临空闲分区
{
pp=p;
p=p->former;
p->size+=pp->size;
pp->next->former=p;
pp=p->next;
p->size+=pp->size;
if(pp->next!=NULL)
{
pp->next->former=p;
}
free(pp);
}
else
{
p->state=0;
p->num=-1;//表示该空闲区未被调用
}
}
p=p->next;
}
if(flag==1)
{
printf("内存分区回收成功!\n");
return 1;
}
else
{
printf("找不到目标作业,内存分区回收失败!\n");
return 0;
}
}
void showArea()
{
printf(" 当前内存分配状况 ");
printf("\n");
printf("起始地址\t分配空间\t工作状态\t作业号\n");
memory *p=LinkList.next;
while(p!=NULL)
{
printf("%5d KB|",p->address);
printf("%5d KB|",p->size);
printf("%5s|",p->state==0?"0":"1");
if(p->num>0)
{
printf("%5d ",p->num);
}
else
{
printf(" ");
}
p=p->next;
}
}
int main()
{
int opt,ope,size,num;
init();
while(1)
{
printf("请选择分区分配算法:1 首次适应算法 2 最佳适应算法\n");
scanf("%d",&opt);
if(opt==1)
{
printf("下面将使用首次适应算法进行分配:\n");
break;
}
else if(opt==2)
{
printf("下面将使用最佳适应算法进行分配\n");
break;
}
else
{
printf("输入错误,请重新输入\n");
break;
}
}
while(1)
{
printf("\n");
printf("1.分配内存 2.回收内存\n");
scanf("%d",&ope);
if(ope==0)
{
break;
}
else if(ope==1)
{
printf("请输入作业号: \n");
scanf("%d",&num);
printf("请输入需要分配的内存: (KB)\n");
scanf("%d",&size);
if(size<=0)
{
printf("内存分配错误\n");
continue;
}
else
{
if(opt==1)
{
FF(num,size);
break;
}
else
{
BF(num,size);
break;
}
}
showArea();
}
else if(ope==2)
{
printf("请输入要回收的作业号:\n");
scanf("%d\n",&num);
release(num);
showArea();
}
else
{
printf("输入错误,请重新输入\n");
}
}
printf("内存分配与回收结束\n");
return 0;
}
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
让showArea函数运行