Array allocation
- Consider the following declarations in C:
char a[12];char *B[8];int C[6];double *D[5];int P[5];short Q[2];int **R[9];double *S[10];short *T[2];| 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
Eand integer indexiare stored in registers%rdxand%rcx, respectively.
-
TODO: exercise 3.37
Nested arrays
- In general, for an array declared as
T D[R][C];
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
Tin bytes. - C = number of columns, R = number of rows

- Example: consider this 2-D array in C
int a[5][3]; //Viewing a as an array of 5 elements, each of which is an array of 3 int's- Suppose xA, i and j are in registers
%rdi,%rsi,%rdxrespectively - Then array element
A[i][j]can be copied to register%eaxby the following code:
leaq (%rsi,%rsi,2), %rax leaq (%rdi,%rax,4), %rax movl (%rax,%rdx,4), %eax