使用c++完成动态分区分配算法,使用首次适应算法,要有交互界面,不是黑框输出要能回收内存,总内存大小1000(B)。空闲分区1号,起始地址100,大小60(B);空闲分区2号,起始地址240,大小120(B);空闲分区3号,起始地址500,大小100(B),空闲分区4号,起始地址760,大小80(B);空闲分区5号,起始地址960,大小40(B)
时间: 2024-02-29 20:53:13 浏览: 56
实验四__动态分区分配方式的模拟_答案
5星 · 资源好评率100%
好的,我了解了您的要求。我会为您提供一个动态分区分配的首次适应算法的 C++ 实现,并且添加交互界面,实现内存的分配和回收。以下是代码实现:
```c++
#include<iostream>
#include<conio.h>
using namespace std;
const int MAX_MEM = 1000; // 总内存大小
const int MIN_MEM = 10; // 最小分配内存大小
int freeMem[MAX_MEM / MIN_MEM]; // 空闲内存块数组,每个元素表示一个内存块,大小为 MIN_MEM
int usedMem[MAX_MEM / MIN_MEM] = {0}; // 已分配内存块数组,每个元素表示一个内存块,大小为 MIN_MEM
const int MAX_FREE_BLOCK = MAX_MEM / MIN_MEM; // 最大空闲块数
int freeBlockNum = 5; // 初始空闲块数
int usedBlockNum = 0; // 初始已使用块数
// 空闲内存块结构体,用于记录空闲块的起始地址和大小
struct FreeBlock
{
int startAddr; // 起始地址
int size; // 大小
} freeBlock[MAX_FREE_BLOCK];
// 初始化空闲内存块数组
void initFreeMem()
{
freeBlock[0].startAddr = 100;
freeBlock[0].size = 60;
freeBlock[1].startAddr = 240;
freeBlock[1].size = 120;
freeBlock[2].startAddr = 500;
freeBlock[2].size = 100;
freeBlock[3].startAddr = 760;
freeBlock[3].size = 80;
freeBlock[4].startAddr = 960;
freeBlock[4].size = 40;
for (int i = 0; i < freeBlockNum; i++)
{
for (int j = freeBlock[i].startAddr / MIN_MEM; j < (freeBlock[i].startAddr + freeBlock[i].size) / MIN_MEM; j++)
{
freeMem[j] = 1;
}
}
}
// 显示内存分配情况
void showMem()
{
cout << "空闲内存块:" << endl;
for (int i = 0; i < freeBlockNum; i++)
{
cout << "空闲块" << i + 1 << ":起始地址 " << freeBlock[i].startAddr << ",大小 " << freeBlock[i].size << endl;
}
cout << "已分配内存块:" << endl;
for (int i = 0; i < MAX_MEM / MIN_MEM; i++)
{
if (usedMem[i] == 1)
{
cout << "已分配块" << i + 1 << ":起始地址 " << i * MIN_MEM << endl;
}
}
}
// 首次适应算法分配内存
void allocateMem()
{
int size;
cout << "请输入分配内存大小(字节):";
cin >> size;
int index = -1;
for (int i = 0; i < freeBlockNum; i++)
{
if (freeBlock[i].size >= size)
{
index = i;
break;
}
}
if (index == -1)
{
cout << "没有足够的内存块可供分配!" << endl;
return;
}
int startIndex = freeBlock[index].startAddr / MIN_MEM;
int endIndex = (freeBlock[index].startAddr + size) / MIN_MEM;
for (int i = startIndex; i < endIndex; i++)
{
freeMem[i] = 0;
}
usedBlockNum += endIndex - startIndex;
usedMem[startIndex] = 1;
if (endIndex == startIndex + freeBlock[index].size / MIN_MEM)
{
for (int i = index; i < freeBlockNum - 1; i++)
{
freeBlock[i] = freeBlock[i + 1];
}
freeBlockNum--;
}
else
{
freeBlock[index].startAddr += size;
freeBlock[index].size -= size;
}
cout << "分配成功!" << endl;
}
// 回收内存
void recycleMem()
{
int addr;
cout << "请输入要回收的内存块起始地址(字节):";
cin >> addr;
int index = addr / MIN_MEM;
if (usedMem[index] == 0)
{
cout << "该内存块未分配,回收失败!" << endl;
return;
}
usedMem[index] = 0;
usedBlockNum--;
int freeBlockIndex = -1;
for (int i = 0; i < freeBlockNum; i++)
{
if (freeBlock[i].startAddr + freeBlock[i].size == index * MIN_MEM)
{
freeBlockIndex = i;
break;
}
}
if (freeBlockIndex != -1)
{
freeBlock[freeBlockIndex].size += MIN_MEM;
}
else
{
freeBlock[freeBlockNum].startAddr = index * MIN_MEM;
freeBlock[freeBlockNum].size = MIN_MEM;
freeBlockNum++;
}
for (int i = index; i < MAX_MEM / MIN_MEM && usedMem[i] == 0; i++)
{
freeBlockIndex = -1;
for (int j = 0; j < freeBlockNum; j++)
{
if (freeBlock[j].startAddr + freeBlock[j].size == (i + 1) * MIN_MEM)
{
freeBlockIndex = j;
break;
}
}
if (freeBlockIndex != -1)
{
freeBlock[freeBlockIndex].size += MIN_MEM;
}
else
{
freeBlock[freeBlockNum].startAddr = (i + 1) * MIN_MEM;
freeBlock[freeBlockNum].size = MIN_MEM;
freeBlockNum++;
}
}
cout << "回收成功!" << endl;
}
int main()
{
initFreeMem();
char op;
while (true)
{
system("cls");
showMem();
cout << "请选择操作(1-分配内存,2-回收内存,0-退出):";
op = _getch();
cout << endl;
switch (op)
{
case '1':
allocateMem();
break;
case '2':
recycleMem();
break;
case '0':
return 0;
default:
cout << "输入错误,请重新输入!" << endl;
break;
}
system("pause");
}
return 0;
}
```
运行程序后,您可以选择分配内存或回收内存,操作完成后,程序会显示当前内存分配情况。
阅读全文