最差拟合算法
Posted
技术标签:
【中文标题】最差拟合算法【英文标题】:Worst fit algorithm 【发布时间】:2021-11-11 20:31:23 【问题描述】:我创建了一个代码,它使用大小为 256 的数组来演示 First Fit 算法,该代码显示了一个地图,显示了如何根据 First Fit 算法存储内存,该代码允许用户删除内存位置也是,我想创建与我的代码相同的概念,但使用,我已经阅读了很多文章并理解了的工作原理,但我想不出一种方法让我的代码使用Worst Fit Algorithm ,在我的代码上使用 First Fit 比 Worst Fit 更容易,有人可以告诉我如何实现我的代码以使用 Worst Fit Algorithm ,将不胜感激。
这是代码:
include <iostream>
#include <cmath>
using namespace std;
int PutInMemory(int memory[], int size)
if (size < 1) cout << "Error!" << endl;
int firstSize = 0;
int j;
for (int i = 0; i < 256; i++)
if (memory[i] < 0 && abs(memory[i]) >= size)
j = i;
firstSize += abs(memory[j]);
break;
if (firstSize < size)
cout << "Out of Memory";
return 0;
if (j + size <= 256)
memory[j] = size;
for (int i = j + 1; i < j + size; i++)
memory[i] = 0;
int i = j + size;
int count = 0;
while (memory[i] <= -1 && i < 256)
count++;
i++;
if (count != 0)
memory[i - 1] = -count;
memory[j + size] = -count;
return j;
else
cout << "Out of memory";
void DelSeg(int memory[], int n)
int count = memory[n];
int prev = 0;
int next = count - 1;
int i = n + 1;
int pos = n;
if (memory[n - 1] < -1 && n != 0)
i += memory[n - 1];
prev = -memory[n - 1];
count -= memory[n - 1];
pos = i;
while (true)
for (i; i < pos + count - 1; i++)
memory[i] = -1;
if (memory[i + 1] < -1)
count += -memory[i + 1] + 1;
next = -memory[i + 1];
else
break;
memory[n - prev] = 0 - count;
memory[n + next] = 0 - count;
void checkMemory(int memory[])
int countFreeSeg = 0;
int countFullSeg = 0;
int countFullMem = 0;
int countFreeMem = 0;
for (int i = 0; i < 256; i++)
if (memory[i] < 0)
if (memory[i] < 0) cout << "Beginning of the adress:" << i << ", ";
int count = 0;
while (memory[i] < 0 && i < 256)
count++;
i++;
countFreeSeg++;
cout << "Size = " << count << endl;
countFreeMem += count;
i--;
cout << "Number of free processes = " << countFreeSeg << endl << endl;
cout << "Number of free memory = " << countFreeMem << endl << endl;
for (int i = 0; i < 256; i++)
if (memory[i] > 0)
cout << "Beginning adress: " << i << ", size - " << memory[i] << endl;
countFullMem += memory[i];
i += memory[i] - 1;
countFullSeg++;
cout << "Number of occupied processes = " << countFullSeg << endl;
cout << "Number of occupied memory = " << countFullMem << endl;
void print(int memory[])
for (int i = 0; i < 256; i++)
cout << memory[i] << " ";
int main()
int memory[256];
memory[0] = -256;
for (int i = 1; i < 256; i++)
memory[i] = -1;
while (true)
system("cls");
cout << "1.Allocate Memory \n2.Free memory segment\n3.Get information about the
memory\n4.Exit" << endl;
int choice;
cin >> choice;
int m = 0;
switch (choice)
case 1:
system("cls");
cout << "Enter the amount of memory to be entered:" << endl;
cin >> m;
cout << PutInMemory(memory, m) << endl;
break;
case 2:
system("cls");
cout << "Enter the starting address of the memory location:" << endl;
cin >> m;
DelSeg(memory, m);
break;
case 3:
checkMemory(memory);
print(memory);
break;
case 4:
system("cls");
exit(0);
break;
default:
cout << "Incorrect entry" << endl;
break;
system("pause");
【问题讨论】:
分解出两种算法中的共同点,并调用专门化不共同点的函数。 @PaulMcKenzie 你刚刚简单描述了软件工程 @PaulMcKenzie 你能告诉我怎么做吗,我不是专家,很高兴向像你这样的专家学习 这是一个你需要重构代码的情况,这样你就不会像现在这样把所有东西都混在一起。我看到的唯一真正区别是您需要以某种方式找到未分配的块。好的,所以创建一个函数调用FindUnallocatedBlock()
或类似返回块的函数。现在什么该函数的作用取决于策略。但其他一切都保持不变。然后当你写这个函数的时候,把它变成一个虚函数,然后覆盖它。请注意,这并不能直接回答您的问题,只是对您当前代码结构的评论。
注意:请不要像这样重复您的问题。原文在这里:***.com/questions/69216299
【参考方案1】:
您的 FirstFit 代码通过查找第一个足够大的内存块来容纳您请求的分配来工作。找到合适的方块后立即停下来。
int firstSize = 0;
int j;
for (int i = 0; i < 256; i++)
if (memory[i] < 0 && abs(memory[i]) >= size)
j = i;
firstSize += abs(memory[j]);
break;
if (firstSize < size)
cout << "Out of Memory";
return 0;
对于 WorstFit,您只需找到最大的可用连续内存块并分配它(如果它足够大)。
int worstSize = 0;
int j;
for (int i = 0; i < 256; i++)
if (memory[i] < 0 && abs(memory[i]) >= worstSize)
j = i;
worstSize = abs(memory[j]);
if (worstSize < size)
cout << "Out of Memory";
return 0;
【讨论】:
以上是关于最差拟合算法的主要内容,如果未能解决你的问题,请参考以下文章