CS 208 s21 — Learning Block #13
Table of Contents
1 Warmup
For each of the following array accesses to the array pictured below, determine if it is a valid access and, if so, what value it returns.1
sea[2][5]
sea[4][-1]
sea[0][19]
Which of the following statements is FALSE?2
sea[4][-2]
is a valid array referencesea[1][1]
makes two memory accessessea[2][1]
will always be a higher address thansea[1][2]
sea[2]
is calculated using only lea
2 Multi-level array exercise
For each of the following array accesses to the array pictured below, determine if it is a valid access and, if so, what value it returns.3
sea[2][3]
sea[1][5]
sea[2][-2]
3 struct exercises
4 Practice
CSPP practice problems 3.41 (p. 268) and 3.45 (p. 275)
Footnotes:
- valid, 9 (start of row 2, then 5 ints forward)
- valid, 5 (start of the non-existent row 4, then 1 int backwards, thereby reference the valid element at the end of row 3)
- valid, 5 (start of row 0, then 19 ints forward, referencing the final int in the array)
2. is the false statement.
The assembly for sea[1][1]
will compute the address of that specific element and then make a single memory access to that address.
- valid, 0
- invalid, past the end of row 1 (and rows are not adjacent in memory in this multilevel array)
- invalid, past the start of row 2 (and rows are not adjacent in memory in this multilevel array)
The int i
is 4 bytes, so it must have an address that's a multiple of 4.
Hence, the compiler adds 3 bytes of padding after the 1-byte char c
to achieve this.
The size of the struct as a whole must be a multiple of the size of the largest field.
Hence, the compiler adds 3 bytes of padding to the end.
S4
has both internal and external fragmentation, while S5
has just external.
struct new { int i; float f; char *c; short s[3]; }
This would eliminate the internal fragmentation and leave just 2 bytes of external fragmentation.
sizeof(struct old)
= 32 bytes (6 bytes of internal fragmentation, 6 bytes external)sizeof(struct new)
= 24 bytes (2 bytes external)