【暑假集训】内存分配

14 篇文章 3 订阅
12 篇文章 0 订阅

内存管理

//alloc n —— 分配n个字节内存,返回已分配块的正整数标识符x(x初始值为0,每//次分配增长1)

//erase x —— 删除标识符x所在的块

//defragment —— 整理空余内存碎片,将所有块尽量靠近内存的开始位置,
//并保持各自的顺序

#include<iostream>
#include<string>
#include<vector>
#include<unordered_map>

const int ORIGIN_POS = 1;

using namespace std;
//Three operations: alloc,erase and defragment

//find all leisure space in memory
void findLeisure(vector<pair<int , int> >&findLeisureSpace,vector<int>& memory)
{
    int n = memory.size();
    for(int i = 0 ; i < n ; i ++)
    {
        if(memory[i] == 0 )
        {
            int j = i + 1;
            if(i == (n - 1) )
            {
                findLeisureSpace.push_back( make_pair(i, i ) );
                break;
            }
            else
                for( ; j < n ; j ++)
                {
                    if(memory[j] != 0 )
                    {
                        findLeisureSpace.push_back(make_pair(i, j - 1) );
                        break;
                    }
                    else if(j == n - 1)
                    {
                        findLeisureSpace.push_back(make_pair(i, j) );
                        break;
                    }
                }
            if( j == n - 1)break;
        }
    }
}

//alloc 
//if fail to allocate spce return false
//else reuturn the index of the allocated block
int alloc(vector<int>& memory, int& currAllocNums, int space)
{
    currAllocNums++;
    bool isAlreadyAlloc = false;
    vector<pair<int , int> >findLeisureSpace;
    findLeisure(findLeisureSpace, memory);
    bool flag = false;
    for(auto& v : findLeisureSpace)
    {
        int ownSpace = v.second - v.first + 1 ;
        if(ownSpace >= space )
        {
            for(int i = v.first ; i < space + v.first ; i ++)
            {
                memory[i] = currAllocNums;
                flag = true;
            }
        }
    }
    if(flag == true)return currAllocNums;
    else
    {
        currAllocNums--;
        return false;
    }
}

//erase the corresponding block
bool erase(vector<int>& memory, int eraseBlock)
{
    int n = memory.size();
    int isErase = false;
    for(int i = 0 ; i < n ; i ++)
    {
        if(memory[i] == eraseBlock)
        {
            memory[i] = 0;
            isErase = true;
        }
    }
    return isErase;
}

void defragment(vector<int>& memory)
{
    int count = 0;
    int n = memory.size();
    // vector<pair<int , int> > findLeisureSpace;
    // findLeisure(findLeisureSpace, memory);
    for(vector<int>::iterator iter = memory.begin() ; iter != memory.end() ; iter++)
    {
        if((*iter) == 0){
            iter = memory.erase(iter);
            count++;
            iter--;
        }
    }
    for(int i = 0 ; i < count ;i ++ )
    {
        memory.push_back(0);
    }
}
int main()
{
    
    int operators = 0 ; 
    int memorySize = 0 ;
    int currAllocNums = 0;
    cin>>operators>>memorySize ;
    vector<int>memory(memorySize,0);
    string operate;
    while(operators --)
    {
        cin>>operate;
        if(operate == "alloc")
        {
            int allocSize = 0;
            cin>>allocSize;
            bool isAlloc = alloc(memory, currAllocNums, allocSize);
            if(!isAlloc )cout<<"NULL"<<endl;
            else cout<<currAllocNums<<endl;
        }
        else if(operate == "erase")
        {
            int eraseBlock = 0;
            cin>>eraseBlock;
            if(! erase(memory,eraseBlock) )
            {
                cout<<"ILLEGAL_ERASE_ARGUMENT"<<endl;
            }
        }
        else{
            defragment(memory);
        }
    }

}

直接模拟就完事了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值