visual c++实现内存分配与回收,要求有退出系统,显示分区,分配分区,回收分区功能,给出完整代码
时间: 2023-12-07 12:05:20 浏览: 33
下面是一个简单的 Visual C++ 实现内存分配与回收的示例程序,包括退出系统、显示分区、分配分区和回收分区功能:
```cpp
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
struct MemoryBlock {
int id;
int size;
bool is_free;
};
vector<MemoryBlock> memory; // 内存块列表
// 初始化内存
void init_memory(int size) {
memory.clear();
MemoryBlock block = { 1, size, true };
memory.push_back(block);
}
// 显示内存分区
void display_memory() {
cout << "Memory Block List:" << endl;
cout << setw(5) << "ID" << setw(10) << "Size" << setw(15) << "Status" << endl;
for (int i = 0; i < memory.size(); i++) {
MemoryBlock block = memory[i];
cout << setw(5) << block.id << setw(10) << block.size << setw(15) << (block.is_free ? "Free" : "Used") << endl;
}
}
// 分配内存
int allocate_memory(int size) {
for (int i = 0; i < memory.size(); i++) {
MemoryBlock block = memory[i];
if (block.is_free && block.size >= size) {
// 分配内存
int id = memory.size() + 1;
MemoryBlock new_block = { id, size, false };
memory.insert(memory.begin() + i, new_block);
// 更新剩余内存块
int remaining_size = block.size - size;
if (remaining_size > 0) {
MemoryBlock remaining_block = { id + 1, remaining_size, true };
memory.insert(memory.begin() + i + 1, remaining_block);
}
memory.erase(memory.begin() + i + 1);
return id;
}
}
return -1; // 分配失败
}
// 回收内存
bool free_memory(int id) {
for (int i = 0; i < memory.size(); i++) {
MemoryBlock block = memory[i];
if (block.id == id && !block.is_free) {
// 回收内存
block.is_free = true;
memory[i] = block;
// 合并空闲内存块
int prev_index = i - 1;
int next_index = i + 1;
if (prev_index >= 0 && memory[prev_index].is_free) {
MemoryBlock prev_block = memory[prev_index];
prev_block.size += block.size;
memory[prev_index] = prev_block;
memory.erase(memory.begin() + i);
i = prev_index;
}
if (next_index < memory.size() && memory[next_index].is_free) {
MemoryBlock next_block = memory[next_index];
block.size += next_block.size;
memory[i] = block;
memory.erase(memory.begin() + next_index);
}
return true;
}
}
return false; // 回收失败
}
int main() {
int size;
cout << "Enter memory size: ";
cin >> size;
init_memory(size);
while (true) {
display_memory();
cout << endl;
cout << "1. Allocate memory" << endl;
cout << "2. Free memory" << endl;
cout << "3. Exit system" << endl;
int choice;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
int alloc_size;
cout << "Enter allocation size: ";
cin >> alloc_size;
int id = allocate_memory(alloc_size);
if (id == -1) {
cout << "Failed to allocate memory" << endl;
} else {
cout << "Allocated memory ID: " << id << endl;
}
break;
case 2:
int free_id;
cout << "Enter memory ID to free: ";
cin >> free_id;
if (free_memory(free_id)) {
cout << "Memory freed successfully" << endl;
} else {
cout << "Failed to free memory" << endl;
}
break;
case 3:
cout << "Exiting system..." << endl;
return 0;
default:
cout << "Invalid choice" << endl;
break;
}
}
return 0;
}
```
该程序使用 `vector` 存储内存块列表,实现了初始化内存、显示内存分区、分配内存和回收内存的功能。在分配内存时,程序会查找空闲内存块并分配给用户请求的大小,如果剩余空间不足,则会将剩余部分分割成新的内存块。在回收内存时,程序会查找与用户请求的内存块相邻的空闲内存块并进行合并。