University of Washington
Memory & data
Roadmap Integers & floats
C: Machine code & C
Java:
x86 assembly
car *c = malloc(sizeof(car)); Car c = new Car(); Procedures & stacks
c->miles = 100; [Link](100);
Arrays & structs
c->gals = 17; [Link](17);
float mpg = get_mpg(c); float mpg = Memory & caches
free(c); [Link](); Processes
Virtual memory
Assembly get_mpg: Memory allocation
pushq %rbp Java vs. C
language: movq %rsp, %rbp
...
popq %rbp
ret
OS:
Machine 0111010000011000
100011010000010000000010
code: 1000100111000010
110000011111101000011111
Computer
system:
Arrays
University of Washington
Section 5: Arrays & Other Data Structures
Array allocation and access in memory
Multi-dimensional or nested arrays
Multi-level arrays
Other structures in memory
Data structures and alignment
Arrays
University of Washington
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
Arrays
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
Reference Type Value
val[4] int 5
val int * x
val+1 int * x+4
&val[2] int * x+8
val[5] int ?? (whatever is in memory at address x + 20)
*(val+1) int 8
val + i int * x + 4*i Arrays
University of Washington
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 };
Arrays
University of Washington
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
Declaration “zip_dig uw” equivalent to “int uw[5]”
Example arrays were allocated in successive 20 byte blocks
Not guaranteed to happen in general
Arrays
University of Washington
Array Accessing Example
zip_dig uw; 9 8 1 9 5
36 40 44 48 52 56
int get_digit
(zip_dig z, int dig)
{
return z[dig];
} Register %edx contains
starting address of array
IA32 Register %eax contains
# %edx = z array index
# %eax = dig Desired digit at
movl (%edx,%eax,4),%eax # z[dig] 4*%eax + %edx
Use memory reference
(%edx,%eax,4)
Arrays
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
Reference Address Value Guaranteed?
uw[3] 36 + 4* 3 = 48 9 Yes
uw[6] 36 + 4* 6 = 60 4 No
uw[-1] 36 + 4*-1 = 32 3 No
cmu[15] 16 + 4*15 = 76 ?? No
No bounds checking
Location of each separate array in memory is not guaranteed
Arrays
University of Washington
Array Loop Example
int zd2int(zip_dig z)
{
int i;
int zi = 0;
for (i = 0; i < 5; i++) {
zi = 10 * zi + z[i];
}
return zi;
}
Arrays
University of Washington
Array Loop Example
int zd2int(zip_dig z)
{
Original int i;
int zi = 0;
for (i = 0; i < 5; i++) {
zi = 10 * zi + z[i];
}
return zi;
}
Transformed int zd2int(zip_dig z)
Eliminate loop variable i, {
use pointer zend instead int zi = 0;
int *zend = z + 4;
Convert array code to do {
pointer code zi = 10 * zi + *z;
Pointer arithmetic on z z++;
Express in do-while form } while (z <= zend);
return zi;
(no test at entrance)
}
Arrays
University of Washington
Array Loop Implementation (IA32)
Registers int zd2int(zip_dig z)
{
%ecx z
int zi = 0;
%eax zi int *zend = z + 4;
%ebx zend do {
Computations zi = 10 * zi + *z;
z++;
10*zi + *z implemented as } while(z <= zend);
*z + 2*(5*zi) return zi;
z++ increments by 4 }
# %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
Arrays