Written Assignment 2

Problem 1

We have discussed why, at least on paper, mark-and-sweep is an inferior garbage collection strategy for languages (such as ones that do not expose pointer operations) that permit implementations to move data in memory. Yet even for such languages, most memory managers use a mark-and-sweep strategy for their large-object space. Give one reason why mark-and-sweep might be preferable on such objects.

State two standard objections to mark-and-sweep, and explain why they don’t apply in this context. For each reason, first state the objection, then explain why it doesn’t apply.


Problem 2

Suppose we have multiple kinds of data of different sizes. In the heap representations we studied in class, the type tag is always at the first address (i.e., at offset 0) that represents a value. Should it matter where the tag resides? For instance, why not put the tag at the last address rather than the first? (The more concrete you are, the more brief you can be.)


Problem 3

Any program that consumes some amount of stack, when converted to CPS and run, suddenly consumes no stack space at all. Why?


Problem 4

A generational garbage collector naturally handles references from newer objects (those allocated more recently) to older ones. A major challenge is keeping track of references that go the other way: from old objects to new. What in the design of a copying generational collector makes it straightforward for the collector to handle references from new to old, rather than vice versa?

Distinguish between variable mutation and value mutation. In variable mutation, we change the value held by a variable itself. In value mutation, the variable still refers to the same object; it’s the content of the object that changes. (For example, set! in Scheme implements variable mutation, while vector-set! implements value mutation.) Which of these does a generational collector need to track specially? For each one, state whether it is or isn’t tracked, with a brief justification for why or why not.


Problem 5

CPS the following Scheme function. You don’t need to CPS primitives such as empty?, first, rest, cons, cons? and <. You may also assume that the function argument to both of the functions is in CPS.

;; filter: (x -> bool) (listof x) -> (listof x)
(define (filter f l)
  (cond
    [(empty? l) empty]
    [else (cond
            [(f (first l)) (cons (first l)
                                 (filter f (rest l)))]
            [else (filter f (rest l))])]))

Now change the following expressions to use the CPSed version of filter.

(define (less-than-three x)
   (< x 3))
(filter less-than-three
        (cons 1 (cons 4 empty))) ;; this evaluates to (list 1)



Problem 6

Convert your answers from problem 5 to use an explicit, vector-based stack in the style of the lecture from 25-10-04. Convert both the function definition and the application expression.