1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
#include <iostream> #include <vector> #include <algorithm> class MemoryAllocator { public: // Constructor MemoryAllocator(int totalSize) : totalSize(totalSize) { memory.resize(totalSize, false); // Initialize memory as free (false) } // Function to allocate memory using First Fit algorithm bool allocateFirstFit(int size) { int start = -1; int freeSize = 0; for (int i = 0; i < totalSize; ++i) { if (!memory[i]) { // Free block if (start == -1) start = i; ++freeSize; if (freeSize == size) { std::fill(memory.begin() + start, memory.begin() + start + size, true); return true; } } else { start = -1; freeSize = 0; } } return false; } // Function to deallocate memory void deallocate(int start, int size) { std::fill(memory.begin() + start, memory.begin() + start + size, false); } // Function to print the current state of memory void printMemory() const { std::cout << "Memory: "; for (bool block : memory) { std::cout << (block ? '1' : '0') << ' '; } std::cout << '\n'; } private: int totalSize; std::vector<bool> memory; // true if allocated, false if free }; int main() { int totalSize, allocationSize; std::cout << "Enter total memory size: "; std::cin >> totalSize; MemoryAllocator allocator(totalSize); while (true) { int choice; std::cout << "\n1. Allocate memory (First Fit)\n2. Deallocate memory\n3. Print memory\n4. Exit\n"; std::cout << "Enter your choice: "; std::cin >> choice; switch (choice) { case 1: std::cout << "Enter size of memory to allocate: "; std::cin >> allocationSize; if (allocator.allocateFirstFit(allocationSize)) { std::cout << "Memory allocated successfully.\n"; } else { std::cout << "Not enough memory available.\n"; } break; case 2: int start, size; std::cout << "Enter starting index and size of memory to deallocate: "; std::cin >> start >> size; allocator.deallocate(start, size); std::cout << "Memory deallocated successfully.\n"; break; case 3: allocator.printMemory(); break; case 4: return 0; default: std::cout << "Invalid choice. Please try again.\n"; } } } |
Explanation
- Class Definition (
MemoryAllocator
):- Private Members:
totalSize
: Total size of the memory.memory
: A vector of booleans representing memory blocks;true
for allocated andfalse
for free.
- Public Methods:
allocateFirstFit(int size)
: Allocates a contiguous block of memory of the given size using the First Fit algorithm.- Searches for the first block of sufficient size and marks it as allocated.
- Returns
true
if successful, otherwisefalse
.
deallocate(int start, int size)
: Frees a block of memory starting at the given index for the specified size.printMemory() const
: Prints the current state of memory (1 for allocated, 0 for free).
- Private Members:
main
Function:- Prompts the user for the total memory size and creates a
MemoryAllocator
object. - Provides a menu for:
- Allocating memory using the First Fit algorithm.
- Deallocating memory.
- Printing the current state of memory.
- Exiting the program.
- Handles user input to perform the chosen operations and manage memory.
- Prompts the user for the total memory size and creates a