CS360 Midterm Exam #1. February 21, 2017. James S. Plank

Answers and Grading

The programs for all five problems are here (even questions four and five). Compile them with the -m32 flag to force them to use 4-byte pointers. I was (in my opinion) quite generous with partial credit.

Question 1: 20 points

Grading

2 points each for lines 1 through 7. 1.5 points each for lines 8-B.


Question 2: 20 points

Here's ASCII art for x, y and z: (using an asterisk for the NULL character):
0123456789ABCDEFGHIJ0123456789*
^         ^         ^
x         y         z
The strcpy(z, "-") command turns it into the following:
0123456789ABCDEFGHIJ-*23456789*
^         ^         ^
x         y         z
And the strcat(y, "#") command turns it into the following:
0123456789ABCDEFGHIJ-#*3456789*
^         ^         ^
x         y         z
Finally, the two lines that set "*y" and "z[5]":
0123456789*BCDEFGHIJ-#*34*6789*
^         ^         ^
x         y         z
Below, I show the five values of x in the for loop:
0123456789*BCDEFGHIJ-#*34*6789*
^      ^      ^      ^      ^
The answer is:
UNIX> a.out
0123456789
789
EFGHIJ-#
#
89
UNIX>

Grading

Four points per line.


Question 3: 15 points with 6 extra credit

If we look at the eight bytes of i, they are stored in little endian:

i[0]
0x67 0xef 0xcd 0xab
i[1]
0xeb 0x24 0xda 0x13

So, c[0] through c[3] are the four bytes of i[0], and c[4] through c[7] are the four bytes of i[1]. Thus, the first two lines are:

1: 0x67 0xef 0xcd 0xab
2: 0xeb 0x24 0xda 0x13
Now, when we perform the memcpy this copies c[2] through c[5] into v:

v
0xcd 0xab 0xec 0x24

When we print v in hex, it prints the above four bytes in little endian:

3: 0x24ecabcd
The next statements copy c[0] and c[1] into the first two bytes of v (remember, vp points to the first byte of v), and c[6] and c[7] into the last two bytes of v. This turns v into:

v
0x67 0xef 0xda 0x13

When we print v in hex, it prints the above four bytes in little endian:

4: 0x13daef67
Now, when we add 0x10101 to i[0] that increments each of the first three bytes by one. i[0] is now:

i[0]
0x68 0xf0 0xce 0xab

So, when we print out the bytes, we get:

5: 0x68 0xf0 0xce 0xab
Finally, when we add one to c[4] and two to c[5], the state of i becomes:

i[0]
0x68 0xf0 0xce 0xab
i[1]
0xec 0x26 0xda 0x13

When we print the two values in i, they are printed in little endian:

6: 0xabcef068
7: 0x13da26ec
Putting it all together, the answer is:
UNIX> gcc -m32 q3.c
UNIX> a.out
1: 0x67 0xef 0xcd 0xab
2: 0xeb 0x24 0xda 0x13
3: 0x24ebabcd
4: 0x13daef67
5: 0x68 0xf0 0xce 0xab
6: 0xabcef068
7: 0x13da26ec
UNIX> 

Grading

Three points per line.


Question 4: 20 points

You can verify this by compiling and running the program:
UNIX> gcc -m32 q4.c
UNIX> a.out
0x78e51120 2.27938697e+274    2028278064    0x78e51130    '0' | -- | -- |'x' 
0x78e51124                    2028278084    0x78e51144    'D' | -- | -- |'x' 
0x78e51128 2.27936055e+274    2028278088    0x78e51148    'H' | -- | -- |'x' 
0x78e5112c                    2028278068    0x78e51134    '4' | -- | -- |'x' 
0x78e51130 2.27934735e+274    2028278080    0x78e51140    '@' | -- | -- |'x' 
0x78e51134                    2028278060    0x78e5112c    ',' | -- | -- |'x' 
0x78e51138 2.27938697e+274    2028278064    0x78e51130    '0' | -- | -- |'x' 
0x78e5113c                    2028278084    0x78e51144    'D' | -- | -- |'x' 
0x78e51140 2.27940017e+274    2028278072    0x78e51138    '8' | -- | -- |'x' 
0x78e51144                    2028278092    0x78e5114c    'L' | -- | -- |'x' 
0x78e51148 2.27939357e+274    2028278072    0x78e51138    '8' | -- | -- |'x' 
0x78e5114c                    2028278088    0x78e51148    'H' | -- | -- |'x' 
1: 0
2: @
3: 8
4: 2028278088
5: 2028278092
6: 2028278088
7: 2.27934735e+274
8: 2.27939357e+274
9: 2.27938697e+274
A: 2.27939357e+274
UNIX> 

Grading

Two points per line.


Question 5: 20 points

The struct ms data structure consumes 16 bytes: Here is an explanation of each line of output: To summarize:
UNIX> gcc -m32 q5.c
UNIX> a.out
0x61626324    1633837882    0x6162633a    ':' |'c' |'b' |'a' 
0x61626328    1633837928    0x61626368    'h' |'c' |'b' |'a' 
0x6162632c    1633837875    0x61626333    '3' |'c' |'b' |'a' 
0x61626330    1633837868    0x6162632c    ',' |'c' |'b' |'a' 
0x61626334    1633837870    0x6162632e    '.' |'c' |'b' |'a' 
0x61626338    1633837934    0x6162636e    'n' |'c' |'b' |'a' 
0x6162633c    1633837934    0x6162636e    'n' |'c' |'b' |'a' 
0x61626340    1633837904    0x61626350    'P' |'c' |'b' |'a' 
0x61626344    1633837887    0x6162633f    '?' |'c' |'b' |'a' 
0x61626348    1633837928    0x61626368    'h' |'c' |'b' |'a' 
0x6162634c    1633837863    0x61626327    ''' |'c' |'b' |'a' 
0x61626350    1633837872    0x61626330    '0' |'c' |'b' |'a' 
0x61626354    1633837909    0x61626355    'U' |'c' |'b' |'a' 
0x61626358    1633837933    0x6162636d    'm' |'c' |'b' |'a' 
0x6162635c    1633837860    0x61626324    '$' |'c' |'b' |'a' 
0x61626360    1633837880    0x61626338    '8' |'c' |'b' |'a' 
0x61626364    1633837879    0x61626337    '7' |'c' |'b' |'a' 
0x61626368    1633837890    0x61626342    'B' |'c' |'b' |'a' 
0x6162636c    1633837903    0x6162634f    'O' |'c' |'b' |'a' 
0x61626370    1633837900    0x6162634c    'L' |'c' |'b' |'a' 
0x61626374    1633837906    0x61626352    'R' |'c' |'b' |'a' 
0x61626378    1633837944    0x61626378    'x' |'c' |'b' |'a' 
0x6162637c    1633837927    0x61626367    'g' |'c' |'b' |'a' 

1: 0x61626324
2: : c b a
3: 1633837928
4: a . c b

5: 0x61626334
6: . c b a
7: 1633837934
8: b a L c

9: 0x61626364
A: 7 c b a
B: 1633837890
C: a 0 c b

D: 0x6162632c
E: 3 c b a
F: 1633837868
G: b a , c

H: 0x61626368
I: B c b a
J: 1633837903
K: ' c b a

UNIX> 

Grading

1 point per line.