This is not the current version of the class.

Problem set 1 Hints

So you’re not sure how to start problem set 1! Not a problem. The problem set description is long.

One key to attacking a large problem is to divide it into smaller pieces. In this class, those pieces are often individual tests—for this pset, test01, test02, etc. Try to pass the tests in order. Make incremental progress.

test01

“You’ll want to examine the test programs to figure out what they’re doing and what output is expected.”

Here is test01.cc.

#include "m61.hh"
#include <cstdio>
// Check that statistics are initially zero.

int main() {
    m61_print_statistics();
}

// Lines starting with "//!" define the expected output for this test.

//! alloc count: active          0   total          0   fail          0
//! alloc size:  active          0   total          0   fail          0

Now, here is a detective-style walk through the code that presents one way to pass the test. It goes very gradually—that’s the point. As you pass more tests, you will gain more familiarity with the code base, and future tests will go faster. Try to answer the following questions yourself before looking at the solutions.

Question. Which m61 functions are called by main?

Question. Given this, which m61 functions are relevant to the expected output?

Question. Does this test ever call m61_malloc or m61_free?

Question. What output does the test expect?

Question. What output does the test generate in the handout code?

Question. How close is the output format to the expected output format?

Question. What is the source of the printed numbers?

Question. Does that weird number have any meaning?

Question. Is the m61_statistics structure documented?

Question. Why does m61_get_statistics produce statistics with that weird number in it?

Question. How can you make the statistics have the right numbers?

test02

#include "m61.hh"
#include <cstdio>
// Check total allocation count statistic.

int main() {
    for (int i = 0; i != 10; ++i) {
        (void) malloc(1);
    }
    m61_print_statistics();
}

// In expected output, "???" can match any number of characters.

//! alloc count: active        ???   total         10   fail        ???
//! alloc size:  active        ???   total        ???   fail        ???

Question. What does this test do?

Question. Does this test ever call m61_malloc or m61_free?

Question. What output is expected?

Question. What output is generated?

Question. What part of the generated output is wrong?

Question. What is the source of the problematic part of the output?

Question. Is the meaning of that problematic output specified anywhere? What is it?

Question. Can this problem be solved by changing just m61_get_statistics?

Question. Can this problem be solved by changing just local variables?

Question. Are global variables allowed in this problem set?

Yes!

Question. How can you make the statistics have the right numbers?

Question. Can you simplify your solution—especially for future tests—with a little advance planning?