07 Arrays
07 Arrays
Computer
system:
Array Allocation
¢ Basic Principle
§ T A[N];
§ Array of data type T and length N
§ Contiguously allocated region of N * sizeof(T) bytes
char string[12];
x x + 12
int val[5];
x x+4 x+8 x + 12 x + 16 x + 20
double a[3];
x x+8 x + 16 x + 24
char* p[3]; IA32
(or char *p[3];)
x x+4 x+8 x + 12
x86-64
x x+8 x + 16 x + 24
Autumn 2013 Arrays & structs 3
University of Washington
Array Access
¢ Basic Principle
§ T A[N];
§ Array of data type T and length N
§ Identifier A can be used as a pointer to array element 0: Type T*
int val[5]; 9 8 1 9 5
x x+4 x+8 x + 12 x + 16 x + 20
Array Access
¢ Basic Principle
§ T A[N];
§ Array of data type T and length N
§ Identifier A can be used as a pointer to array element 0: Type T*
int val[5]; 9 8 1 9 5
x x+4 x+8 x + 12 x + 16 x + 20
Array Example
typedef int zip_dig[5];
zip_dig cmu = { 1, 5, 2, 1, 3 };
zip_dig uw = { 9, 8, 1, 9, 5 }; initialization
zip_dig ucb = { 9, 4, 7, 2, 0 };
int uw[5] …
Array Example
typedef int zip_dig[5];
zip_dig cmu = { 1, 5, 2, 1, 3 };
zip_dig uw = { 9, 8, 1, 9, 5 };
zip_dig ucb = { 9, 4, 7, 2, 0 };
zip_dig cmu; 1 5 2 1 3
16 20 24 28 32 36
zip_dig uw ; 9 8 1 9 5
36 40 44 48 52 56
zip_dig ucb; 9 4 7 2 0
56 60 64 68 72 76
int get_digit
(zip_dig z, int dig)
{
return z[dig];
} n Register %edx contains
starting address of array
IA32 n Register %eax contains
# %edx = z array index
# %eax = dig n Desired digit at
movl (%edx,%eax,4),%eax # z[dig] 4*%eax + %edx
n Use memory reference
(%edx,%eax,4)
Autumn 2013 Arrays & structs 8
University of Washington
Referencing Examples
zip_dig cmu; 1 5 2 1 3
16 20 24 28 32 36
zip_dig uw; 9 8 1 9 5
36 40 44 48 52 56
zip_dig ucb; 9 4 7 2 0
56 60 64 68 72 76
zi = 10*981 + 9 = 9819
zi = 10*9819 + 5 = 98195
9 8 1 9 5
Autumn 2013 Arrays & structs 10
University of Washington
# %ecx = z
xorl %eax,%eax # zi = 0
leal 16(%ecx),%ebx # zend = z+4
.L59:
leal (%eax,%eax,4),%edx # zi + 4*zi = 5*zi
movl (%ecx),%eax # *z
addl $4,%ecx # z++
leal (%eax,%edx,2),%eax # zi = *z + 2*(5*zi)
cmpl %ebx,%ecx # z : zend
jle .L59 # if <= goto loop
Autumn 2013 Arrays & structs 12
University of Washington
9 8 1 9 5 9 8 1 0 5 9 8 1 0 3 9 8 1 1 5
4*R*C Bytes
Autumn 2013 Arrays & structs 16
University of Washington
int A[R][C];
A A A A A A
[0] ••• [0] • • • [i] ••• [i] • • • [R-1] ••• [R-1]
[0] [C-1] [0] [C-1] [0] [C-1]
A A+i*C*4 A+(R-1)*C*4
# %eax = index
leal (%eax,%eax,4),%eax # 5 * Translation?
index
leal sea(,%eax,4),%eax # sea + (20 * index)
# %eax = index
leal (%eax,%eax,4),%eax # 5 * index
leal sea(,%eax,4),%eax # sea + (20 * index)
¢ Row Vector
§ sea[index] is array of 5 ints
§ Starting address sea+20*index
¢ IA32 Code
§ Computes and returns address
§ Compute as sea+4*(index+4*index)=sea+20*index
Autumn 2013 Arrays & structs 20
University of Washington
int A[R][C];
A A A A A
[0] ••• [0] • • • ••• [i] ••• • • • [R-1] ••• [R-1]
[0] [C-1] [j] [0] [C-1]
A A + i*C*4 A + (R-1)*C*4
?
Autumn 2013 Arrays & structs 21
University of Washington
int A[R][C];
A A A A A
[0] ••• [0] • • • ••• [i] ••• • • • [R-1] ••• [R-1]
[0] [C-1] [j] [0] [C-1]
A A + i*C*4 A + (R-1)*C*4
A + i*C*4 + j*4
Autumn 2013 Arrays & structs 22
University of Washington
# %ecx = dig
# %eax = index
leal 0(,%ecx,4),%edx # 4*dig
leal (%eax,%eax,4),%eax # 5*index
movl sea(%edx,%eax,4),%eax # *(sea + 4*dig + 20*index)
¢ Array Elements
§ sea[index][dig] is int
§ Address: sea + 20*index + 4*dig
¢ IA32 Code
§ Computes address sea + 4*dig + 4*(index+4*index)
§ movl performs memory reference
Autumn 2013 Arrays & structs 23
University of Washington
zip_dig
sea[4]; 9 8 1 9 5 9 8 1 0 5 9 8 1 0 3 9 8 1 1 5
N-dimensional arrays…
double heatMap3D[1024][1024][1024];
&heapMap3D[300][800][2] = ?
int univ2D[3] = {
{ 9, 8, 1, 9, 5 },
{ 1, 5, 2, 1,
{ 9, 4, 7, 2,
3
0
},
}
Same thing as a 2D array?
};
} 60 64 68 72 76 80
# %ecx = index
# %eax = dig
leal 0(,%ecx,4),%edx # 4*index
movl univ(%edx),%edx # Mem[univ+4*index]
movl (%edx,%eax,4),%eax # Mem[...+4*dig]
¢ Computation (IA32)
§ Element access Mem[Mem[univ+4*index]+4*dig]
§ Must do two memory reads
§ First get pointer to row array
§ Then access element within array
Autumn 2013 Arrays & structs 28
University of Washington
60 64 68 72 76 80
a b k-th column
x
i-th row
Autumn 2013 Arrays & structs 32
University of Washington
movl 12(%ebp),%eax # i
movl 8(%ebp),%edx # a
imull 20(%ebp),%eax # n*i
addl 16(%ebp),%eax # n*i+j
movl (%edx,%eax,4),%eax # Mem[a+4*(i*n+j)]
Arrays in C
¢ Contiguous allocations of memory
¢ No bounds checking
¢ Can usually be treated like a pointer to first element
¢ int a[4][5] => array of arrays
§ all levels in one contiguous block of memory
¢ int* b[4] => array of pointers to arrays
§ first level in one contiguous block of memory
§ parts anywhere in memory
Structures
struct rec { Memory Layout
int i;
int a[3]; i a p
int* p; 0 4 16 20
};
¢ Characteristics
§ Contiguously-allocated region of memory
§ Refer to members within structure by names
§ Members may be of different types
Structures
struct rec {
¢ Accessing Structure Member int i;
int a[3];
§ Given an instance of the struct, we can use
int* p;
the . operator, just like Java: };
§ struct rec r1; r1.i = val;
§ What if we have a pointer to a struct: struct rec* r = &r1;
Structures
struct rec {
¢ Accessing Structure Member int i;
int a[3];
§ Given an instance of the struct, we can use
int* p;
the . operator, just like Java: };
§ struct rec r1; r1.i = val;
§ What if we have a pointer to a struct: struct rec* r = &r1;
§ Using * and . operators: (*r).i = val;
§ Or, use -> operator for short: r->i = val;
§ Pointer indicates first byte of structure; access members with offsets
void
set_i(struct rec* r, IA32 Assembly
int val) # %eax = val
{ # %edx = r
r->i = val; movl %eax,0(%edx) # Mem[r+0] = val
}
&(r->a[idx])
# %ecx = idx
# %edx = r
leal 0(,%ecx,4),%eax # 4*idx
leal 4(%eax,%edx),%eax # r+4*idx+4
Autumn 2013 Arrays & structs 38
University of Washington
&(r->a[idx])
# %ecx = idx OR
# %edx = r
leal 4(%edx,%ecx,4),%eax # r+4*idx+4
# %ecx = idx
# %edx = r
movl 4(%edx,%ecx,4),%eax # Mem[r+4*idx+4]
¢ Aligned Data
§ Primitive data type requires K bytes
§ Address must be multiple of K
c 7 bytes v i
p+0 p+8 p+16 p+20
Multiple of 8 Multiple of 4
internal fragmentation
Autumn 2013 Arrays & structs 42
University of Washington
Alignment Principles
¢ Aligned Data
§ Primitive data type requires K bytes
§ Address must be multiple of K
¢ Aligned data is required on some machines; it is advised
on IA32
§ Treated differently by IA32 Linux, x86-64 Linux, Windows, Mac OS X, …
¢ What is the motivation for alignment?
Alignment Principles
¢ Aligned Data
§ Primitive data type requires K bytes
§ Address must be multiple of K
¢ Aligned data is required on some machines; it is advised
on IA32
§ Treated differently by IA32 Linux, x86-64 Linux, Windows, Mac OS X, …
¢ Motivation for Aligning Data
§ Physical memory is accessed by aligned chunks of 4 or 8 bytes (system-
dependent)
§ Inefficient to load or store datum that spans these boundaries
§ Also, virtual memory is very tricky when datum spans two pages (later…)
¢ Compiler
§ Inserts padding in structure to ensure correct alignment of fields
§ sizeof() should be used to get true size of structs
Autumn 2013 Arrays & structs 44
University of Washington
Saving Space
¢ Put large data types first:
struct S1 { struct S2 {
char c; double v;
double v; int i;
int i; char c;
} * p; } * q;
v i c But actually…
q+0 q+8 q+12 q+13
c 7 bytes v i
p+0 p+8 p+16 p+20
v i c 3 bytes
q+0 q+8 q+12 q+16
Arrays of Structures
¢ Satisfy alignment requirement struct S2 {
for every element double v;
int i;
¢ How would accessing an element work? char c;
} a[10];
v i c 3 bytes
a+16 a+24 a+28 a+32
Unions
¢ Allocated according to largest element
¢ Can only use one member at a time
union U {
char c;
int i[2]; c
double v; i[0] i[1]
} *up;
v
struct S { up+0 up+4 up+8
char c;
int i[2];
double v;
} *sp;
hw_register reg;
reg.byte = 0x3F; // 001111112
reg.bits.b2 = 0; // 001110112
reg.bits.b3 = 0; // 001100112
unsigned short a = reg.byte;
printf("0x%X\n", a); // output: 0x33
Summary
¢ Arrays in C
§ Contiguous allocations of memory
§ No bounds checking
§ Can usually be treated like a pointer to first element
¢ Structures
§ Allocate bytes in order declared
§ Pad in middle and at end to satisfy alignment
¢ Unions
§ Provide different views of the same memory location