Compilers Principles, Techniques, & Tools (purple dragon book) second edition exercise answers

Exercises for Section 7.4

7.4.1

Suppose the heap consists of seven chunks, starting at address 0. The sizes of the chunks, in order, are 80, 30, 60, 50, 70, 20, 40 bytes. When we place an object in a chunk, we put it at the high end if there is enough space remaining to form a smaller chunk (so that the smaller chunk can easily remain on the linked list of free space) . However , we cannot tolerate chunks of fewer that 8 bytes, so if an object is almost as large as the selected chunk, we give it the entire chunk and place the object at the low end of the chunk. If we request space for objects of the following sizes: 32, 64, 48, 16, in that order, what does the free space list look like after satisfying the requests, if the method of selecting chunks is

  1. First fit.
  2. Best fit.

Answer

values in parentheses are sizes actually in use

  1. First fit.

    48, 32(32), 14, 16(16), 60, 50(48), 70(64), 20, 40

  2. Best fit.

    80, 30, 60, 50(48), 70(64), 20(16), 8, 32(32)