7 - Data Structures 1
7 - Data Structures 1
CS 215 Lecture 7
Motivation
Real programs use abstractions like
lists, trees, stacks, and queues.
The data associated with these
abstractions must be ordered within
memory.
Assembly language does not provide
convenient ways to access data in
memory like high-level languages do.
It therefore necessary to calculate
explicitly the locations of data within the
structure.
Memory
target
base address address ofelement
target element
Finding the element address
The formula for finding the address of an
element in an array can be generalized
to
char = 1 byte
integer = 4 bytes
i = b + s*n
where b = base address
s = size of each element
n = the number of elements between
the target element and the base
address
2 x 7 array
Example 15.4
7 x 2 x (4 bytes)
Storage Order
1
Storage Order
1
Accessing elements in 2D arrays
i = b + s*n
Row-major order: e = 4, k = 1, n’ = 2
Column-major order: e = 3, k =2, n’ = 1
e*k + n’ = 7
skip 7 elements 0 1 2 3 target
0 element
1