Array allocation
- Consider the following declarations in C:
Array | Element size | Total size | Start address | Element i |
---|---|---|---|---|
A | 1 | 12 | xA | xA + i |
B | 8 | 64 | xB | xB + 8i |
C | 4 | 24 | xC | xC + 4i |
D | 8 | 40 | xD | xD + 8i |
P | 4 | 20 | xP | xP + 4i |
Q | 2 | 4 | xQ | xQ + 2i |
R | 8 | 72 | xR | xR + 8i |
S | 8 | 80 | xS | xS + 8i |
T | 8 | 16 | xT | xT + 8i |
Pointer arithmetic
-
Example: suppose the starting address of integer array
E
and integer indexi
are stored in registers%rdx
and%rcx
, respectively. -
TODO: exercise 3.37
Nested arrays
- In general, for an array declared as
array element D[i][j]
memory address is calculated as:
- &D[i][j] = xD + L(C*i + j)
- where L is the size of data type
T
in bytes. - C = number of columns, R = number of rows
- Example: consider this 2-D array in C
- Suppose xA, i and j are in registers
%rdi
,%rsi
,%rdx
respectively - Then array element
A[i][j]
can be copied to register%eax
by the following code: