Skip to content

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];
ArrayElement sizeTotal sizeStart addressElement i
A112xAxA + i
B864xBxB + 8i
C424xCxC + 4i
D840xDxD + 8i
P420xPxP + 4i
Q24xQxQ + 2i
R872xRxR + 8i
S880xSxS + 8i
T816xTxT + 8i

Pointer arithmetic

  • Example: suppose the starting address of integer array E and integer index i are stored in registers %rdx and %rcx, respectively.

    Jump Instructions

  • 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 T in bytes.
  • C = number of columns, R = number of rows

Element of array in row-major order

  • 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, %rdx respectively
  • Then array element A[i][j] can be copied to register %eax by the following code:
leaq (%rsi,%rsi,2), %rax
leaq (%rdi,%rax,4), %rax
movl (%rax,%rdx,4), %eax

Fixed-size arrays

Variable-size arrays