Escolar Documentos
Profissional Documentos
Cultura Documentos
Name:
Student ID # :
Problem 1
20 points
_______________________________
Problem 2
30 points
_______________________________
Problem 3
30 points
_______________________________
Problem 4
20 points
_______________________________
Total
100 points
_______________________________
Page 2
Name: ____________________________________________
Part B (5 points): The following code has one error. Identify the error and indicate how
to fix the error.
void player_free (player_t* p)
{
free (p);
free (p->name);
}
Part C (5 points): Assuming that our compiler uses 16-bit 2s complement numbers to
represent ints, give values for v1 and v2 such that v1 < v2, but the less_than
function below returns 0.
int less_than (int v1, int v2)
{
return (v1 v2 < 0);
}
Page 3
Name: ____________________________________________
Problem 1, continued:
Part D (5 points): The following code has one error. Identify the error and indicate how
to fix the error.
int* split_digits (int value)
{
int digits[12];
int cnt = 0;
if (0 <= value) {
while (10 <= value) {
digits[cnt++] = (value % 10);
value = (value / 10);
}
digits[cnt++] = value;
}
digits[cnt] = -1;
return digits;
}
Page 4
Name: ____________________________________________
h
e
l
l
o
E
C
E
1
9
0
for (a =
}
}
; a++) {
Page 5
Name: ____________________________________________
Problem 2, continued:
Parts B, C, and D ask about the C function count below.
int count (char* s, int inside)
{
if ('\0' == *s) {
return inside;
}
if ('.' == *s) {
if (inside) {
return (count (s + 1, 0) + 1);
}
return count (s + 1, 0);
}
return count (s + 1, 1);
}
Page 6
Name: ____________________________________________
Part A (10 points): Write the contents of the arrays base and added just before main
returns in the program above.
base:
added: {
_________ , __________ }
Page 7
Name: ____________________________________________
Problem 3, continued:
Part B (20 points): The diagram below shows the state of the stack just before the
function merge returns (before execution of the return statement) in the program on
the previous page. Most of merges stack frame and some of mains stack frame have
been labeled, and some of the values have been filled in for you.
Fill in the rest of the stack frame for merge by writing both the values and the meanings
for the remaining locations into the boxes. For example, you need to write in the values
of the parameters passed to merge. If you can not know the value at an address or the
meaning of the value stored there, write unknown. Blank boxes will not receive points.
xC771
R6
xC772
R5
xC773
xC774
x3443
xC775
return address
return value
xC776
xC777
xC778
xC779
xC77A
xC77B
xC77C
x0013
xC77D
x0002
xC77E (unknown)
xC77F
42
(unknown)
base[0]
xC780
base[1]
xC781
base[2]
xC782
base[3]
xC783
added[0]
xC784
19
added[1]
xC785
sum
Page 8
Name: ____________________________________________
to->prev
return 1;
}
= __________________;