In high-performance firmware (like network drivers), creating and destroying thousands of packets per second using malloc causes fragmentation and CPU overhead. A Static Object Pool solves this by pre-allocating a fixed array of objects at compile time and managing them manually.
Your task is to implement a MessagePool class.
Message objects.bool flags (in_use) to track availability.static Message* allocate():i where in_use[i] is false.in_use[i] = true.pool[i].nullptr.static void release(Message* m):i based on the pointer address (pointer arithmetic).in_use[i] = false.Program Flow:
N (number of commands).N times.cmd.cmd is "ALLOC":id.allocate(). If successful, set msg->id = id and print Allocated Slot <index>. Else print Pool Full.cmd is "FREE":slot_index.release() on the address of that specific slot (simulating a pointer passed back from the application). Print Freed Slot <index>.Input Format:
N (1 to 20).N lines: String cmd ("ALLOC", "FREE"), followed by values.Output Format:
Allocated Slot <index> or Pool FullFreed Slot <index>Example: Example 1
Input:
5
ALLOC 100
ALLOC 200
ALLOC 300
ALLOC 400
FREE 1Output:
Allocated Slot 0
Allocated Slot 1
Allocated Slot 2
Pool Full
Freed Slot 1Constraints:
static arrays for storage.new or malloc.
Input
5 ALLOC 100 ALLOC 200 ALLOC 300 ALLOC 400 FREE 1
Expected Output
Allocated Slot 0 Allocated Slot 1 Allocated Slot 2 Pool Full Freed Slot 1