CS360 Midterm Exam. March 14, 2013. James S. Plank

Answers and Grading


Question 1: 15 Points

Let's start as always by labeling some pointers in memory.

That gets us the three lines of output:

A[0] : 1048972
A[1] : 1048948
A[2] : 1048940
For the lines involving b, we need to look at what b[0], b[1] and b[2] point to:

That allows us to print the next three sets of lines:

B[0] : 0x100174
B[0][0] : 1048956
B[0][1] : 1048924
B[0][2] : 1048936

B[1] : 0x100140
B[1][0] : 1048948
B[1][1] : 1048940
B[1][2] : 1048936

B[2] : 0x100150
B[2][0] : 1048956
B[2][1] : 1048944
B[2][2] : 1048948
The next loop is a little tricky. This is not a regular doubly-linked list. To figure it out, start with p equaling head, and treat the 12 bytes as a flink, blink and val as pictured below, lableled "first p." We chase those flink pointers to get each successive p:

That gives us the next three lines of output:

0: p: 0x100178  p->val: 1048884
1: p: 0x10015c  p->val: 1048896
2: p: 0x100140  p->val: 1048936
Then, starting with p = 0x100140, we chase blink pointers, which are the second four bytes of each p. I do this in two drawings, because the last p overlaps with the fifth p:

And that gives us the last three lines of output:

0: p: 0x10016c  p->val: 1048956
1: p: 0x100178  p->val: 1048884
2: p: 0x100168  p->val: 1048952

Grading


Question 2

This is straight from the lecture notes (q2.c):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fields.h"

main()
{
  char *l, *p;
  IS is;

  l = NULL;
  p = NULL;

  is = new_inputstruct(NULL);
  while (get_line(is) >= 0) {
    if (p != NULL) free(p);
    p = l;
    l = strdup(is->text1);
  }
  if (p != NULL) printf("%s", p);
  exit(0);
}

There are of course other ways to do this.

Grading

10 points -- grading programs is more objective, so I handle each exam on a case by case basis.


Question 3

For the answer, go ahead and see the scanned answer when I did this by hand. I decided that the exam was too long, so you didn't have to do part D.

Grading: 4 points per part.


Question 4

Let's build this up. The fp and sp start at 0xfff4a0. main() will call push #4 to allocate i, and then it pushes 15 on the stack, as printf()'s third argument. Then it pushes 8 on the stack for the b(8) call, and it calls jsr b. This will push 0x1110 -- four bytes after the pc, and the fp -- 0xfff4a0 onto the stack. So, when b(8) starts to run, the stack looks like:

Now, we're in b(8). It decrements the stack pointer 4 bytes to allocate j and sets j to three. Since the if statement is false, it recursively calls b on 8+5 = 13. That part of the stack is:

Now, we're in b(13). Again, it allocates j and sets it to three. Since i is indeed greater than 10, it now pushes &i and i onto the stack, and calls jsr a:

Finally, we're in a(). It allocates k, and calculates i + *jp. Since jp equals 0xfff474, *jp equals 13, so k is set to 26. 27 is put into r0, and we return with the entire stack looking as follows:

Grading


Question 5

You didn't have to do question 5, but here's the answer. It seems sad to waste a perfectly good exam question, but there you have it.