THINK FIRST·CODE LATER

← All labs

Contiguous allocation: first, best, worst and next fit

Problem

Simulate a contiguous memory allocator.

Input: the policy (first, best, worst or next), the memory size, then operations alloc NAME size and free NAME until the end of input. Initially memory is one hole [0, size).

  • Holes are kept sorted by address; a block is always placed at the start of the chosen hole. Ties: the lowest address.
  • Next fit starts searching at the first hole that ends after the end of the last allocated block, and wraps around.
  • On free, merge the freed block with adjacent holes.

Output: alloc A 200 -> [0, 200) or alloc F 180: FAILED (free 360, largest hole 150); free A -> holes [0, 200) [850, 1000). At the end: Final holes: … (or none), Free: X, largest hole: Y, failed requests: Z, and External fragmentation: P% where P = round(100 × (free − largest) / free) (0 if nothing is free).

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.