C++实现动态分区内存分配与回收模拟程序

本博客介绍如何使用C++设计和实现一个内存分配与回收程序,该程序利用链表模拟内存存储空间,并采用动态分区存储管理方法进行内存的分配和回收。

代码实现

#include <iostream>
#include <random>

// 内存块结构体
struct MemoryBlock {
    int start;
    int end;
    int processId;
    MemoryBlock* next;
};

// 内存管理器类
class MemoryManager {
private:
    int memorySize;
    MemoryBlock* freeMemory;
    MemoryBlock* allocatedMemory;

public:
    // 构造函数,初始化内存空间
    MemoryManager(int size) {
        memorySize = size;
        freeMemory = new MemoryBlock();
        freeMemory->start = 0;
        freeMemory->end = size;
        freeMemory->processId = -1;
        freeMemory->next = nullptr;
        allocatedMemory = nullptr;
    }

    // 分配内存
    void allocateMemory(int processId, int size) {
        MemoryBlock* currentBlock = freeMemory;
        MemoryBlock* previousBlock = nullptr;

        // 查找合适的空闲内存块
        while (currentBlock) {
            if (currentBlock->end - currentBlock->start >= size) {
                break;
            }

            previousBlock = currentBlock;
            currentBlock = currentBlock->next;
        }

        // 未找到合适的空闲内存块
        if (currentBlock == nullptr) {
            std::cout << '无法为进程' << processId << '分配大小为' << size << '的内存空间' << std::endl;
            return;
        }

        // 创建新的已分配内存块
        MemoryBlock* newBlock = new MemoryBlock();
        newBlock->start = currentBlock->start;
        newBlock->end = currentBlock->start + size;
        newBlock->processId = processId;
        newBlock->next = nullptr;

        // 更新空闲内存块信息
        currentBlock->start += size;

        // 将新内存块插入已分配内存链表
        if (allocatedMemory == nullptr) {
            allocatedMemory = newBlock;
        } else {
            MemoryBlock* lastBlock = allocatedMemory;
            while (lastBlock->next) {
                lastBlock = lastBlock->next;
            }
            lastBlock->next = newBlock;
        }

        std::cout << '为进程' << processId << '成功分配大小为' << size << '的内存空间' << std::endl;
    }

    // 回收内存
    void deallocateMemory(int processId) {
        MemoryBlock* previousBlock = nullptr;
        MemoryBlock* currentBlock = allocatedMemory;

        // 查找目标进程的内存块
        while (currentBlock) {
            if (currentBlock->processId == processId) {
                // 从已分配内存链表中移除目标内存块
                if (previousBlock == nullptr) {
                    allocatedMemory = currentBlock->next;
                } else {
                    previousBlock->next = currentBlock->next;
                }

                currentBlock->next = nullptr;

                // 将回收的内存块插入空闲内存链表
                MemoryBlock* freeBlock = freeMemory;
                while (freeBlock->next && currentBlock->start > freeBlock->next->start) {
                    freeBlock = freeBlock->next;
                }

                currentBlock->next = freeBlock->next;
                freeBlock->next = currentBlock;

                std::cout << '成功回收进程' << processId << '的内存空间' << std::endl;
                return;
            }

            previousBlock = currentBlock;
            currentBlock = currentBlock->next;
        }

        std::cout << '进程' << processId << '没有被分配内存空间' << std::endl;
    }

    // 打印内存使用情况
    void printMemoryStatus() {
        std::cout << '空闲内存空间:' << std::endl;
        MemoryBlock* currentBlock = freeMemory;
        while (currentBlock) {
            std::cout << '[' << currentBlock->start << ' - ' << currentBlock->end << '] -> ';
            currentBlock = currentBlock->next;
        }
        std::cout << 'None' << std::endl;

        std::cout << '已分配内存空间:' << std::endl;
        currentBlock = allocatedMemory;
        while (currentBlock) {
            std::cout << '[' << currentBlock->start << ' - ' << currentBlock->end << '] (进程ID: ' << currentBlock->processId << ') -> ';
            currentBlock = currentBlock->next;
        }
        std::cout << 'None' << std::endl;
    }
};

int main() {
    int memorySize = 1000;
    MemoryManager manager(memorySize);

    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> sizeDist(50, 200);
    std::uniform_int_distribution<> processIdDist(1, 5);

    // 模拟内存分配
    for (int i = 0; i < 5; i++) {
        int processId = processIdDist(gen);
        int memorySize = sizeDist(gen);
        manager.allocateMemory(processId, memorySize);
        manager.printMemoryStatus();
        std::cout << std::endl;
    }

    // 模拟内存回收
    for (int i = 0; i < 5; i++) {
        int processId = processIdDist(gen);
        manager.deallocateMemory(processId);
        manager.printMemoryStatus();
        std::cout << std::endl;
    }

    return 0;
}

代码说明

  1. MemoryBlock 结构体:
    • 表示一个内存块,包含起始地址 (start)、结束地址 (end)、进程 ID (processId) 以及指向下一个内存块的指针 (next)。
  2. MemoryManager 类:
    • memorySize: 总内存大小。
    • freeMemory: 指向空闲内存链表头部的指针。
    • allocatedMemory: 指向已分配内存链表头部的指针。
    • allocateMemory(int processId, int size): 为指定进程分配指定大小的内存。
    • deallocateMemory(int processId): 回收指定进程的内存。
    • printMemoryStatus(): 打印当前内存使用情况,包括空闲内存块和已分配内存块的信息。
  3. main 函数:
    • 创建一个 MemoryManager 对象,设置总内存大小。
    • 使用随机数生成器模拟进程 ID 和内存需求。
    • 循环调用 allocateMemorydeallocateMemory 方法模拟内存分配和回收过程。
    • 每次操作后调用 printMemoryStatus 方法打印当前内存使用情况。

总结

本博客提供了一个简单的C++内存分配与回收程序示例,该程序使用链表模拟内存空间,并利用动态分区存储管理方法演示了进程内存的分配与回收过程。 实际应用中,内存管理算法可能更加复杂,例如首次适应算法、最佳适应算法、最坏适应算法等,本示例仅供学习和参考。


原文地址: https://www.cveoy.top/t/topic/N8S 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录