CS360 Final Exam: December 12, 2000. Question 2

Behold the following implementation of malloc() and free().
char *malloc(int size)
{
  return sbrk(size);
}

void free(char *s)
{
  return;
}
Listed on the right are some possible outcomes of a program that uses this version of malloc().
a. It is going to use much more memory than the regular version of malloc().
b. It is going to use much less memory than the regular version of malloc().
d. It may exit because it runs out of memory.
e. It will very likely exit on a bus error.
f. It may exit on a bus error, but I doubt it.
g. It is not going to exit on a bus error.
h. It is going to perform much slower than were it using the regular version of malloc()/free().

For each part below, circle all of the outcomes listed above that apply. Assume that sbrk() initially returns a multiple of 8, and that our pointers are 4 bytes.

Part i: Suppose we write a program that sorts the lines of a file using the fields library, the red-black tree library, and strdup(). What outcomes apply when this program is executed on a large text file?

Part ii: Suppose we write a program that reads integers from a file, and sorts them using the red-black tree library. What outcomes apply when this program is executed on a large text file?

Part iii: Suppose we write a program that reads doubles from a file, and sorts them using the red-black tree library. What outcomes apply when this program is executed on a large text file?

Part iv: Suppose cat were compiled with the malloc() above. What outcomes apply when cat is executed with a huge input file?

Part v: Suppose netscape were compiled with the malloc() above. What outcomes apply when netscape is given its normal usage?

Part vi: The following program is compiled and executed:

main()
{
  JRB t;  

  t = make_jrb();
  while(1) {
    sleep(1);
    if (!jrb_empty() && lrand48() % 2 == 0) {
      jrb_delete_node(jrb_first(t));
    } else {
      jrb_insert_int(t, lrand48(), JNULL);
    }
  }
}