0% found this document useful (0 votes)
7 views7 pages

lecture_slides_06_063-multilevelarrays

The document discusses multi-level arrays, highlighting their structure and memory allocation in C programming. It explains how multi-level arrays differ from nested arrays, particularly in terms of memory access and element retrieval. Additionally, it emphasizes the lack of bounds checking in C arrays and the implications for memory management.

Uploaded by

yihuangece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

lecture_slides_06_063-multilevelarrays

The document discusses multi-level arrays, highlighting their structure and memory allocation in C programming. It explains how multi-level arrays differ from nested arrays, particularly in terms of memory access and element retrieval. Additionally, it emphasizes the lack of bounds checking in C arrays and the implications for memory management.

Uploaded by

yihuangece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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

Multi-level Arrays
University of Washington

Multi-Level Array Example


zip_dig cmu = { 1, 5, 2, 1, 3 };
zip_dig uw = { 9, 8, 1, 9, 5 };
zip_dig ucb = { 9, 4, 7, 2, 0 };

#define UCOUNT 3
int *univ[UCOUNT] = {uw, cmu, ucb};

Same thing as a 2D array?

Multi-level Arrays
University of Washington

Multi-Level Array Example


 Variable univ denotes
zip_dig cmu = { 1, 5, 2, 1, 3 };
array of 3 elements
zip_dig uw = { 9, 8, 1, 9, 5 };
zip_dig ucb = { 9, 4, 7, 2, 0 };  Each element is a pointer
 4 bytes
#define UCOUNT 3
int *univ[UCOUNT] = {uw, cmu, ucb};  Each pointer points to array
of ints
cmu
1 5 2 1 3
univ
16 20 24 28 32 36
160 36 uw
9 8 1 9 5
164 16
168 56 ucb 36 40 44 48 52 56
9 4 7 2 0
56 60 64 68 72 76

Note: this is how Java represents multi-dimensional arrays.


Multi-level Arrays
University of Washington

Element Access in Multi-Level Array


int get_univ_digit
(int index, int dig)
{
return univ[index][dig];
}

# %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
Multi-level Arrays
University of Washington

Array Element Accesses


Nested array Multi-level array
int get_sea_digit int get_univ_digit
(int index, int dig) (int index, int dig)
{ {
return sea[index][dig]; return univ[index][dig];
} }

Access looks similar, but it isn’t:


Mem[sea+20*index+4*dig] Mem[Mem[univ+4*index]+4*dig]

Multi-level Arrays
University of Washington

Strange Referencing Examples


cmu
1 5 2 1 3
univ
16 20 24 28 32 36
160 36 uw
9 8 1 9 5
164 16
168 56 ucb 36 40 44 48 52 56
9 4 7 2 0
56 60 64 68 72 76

 Reference Address Value Guaranteed?


univ[2][3] 56+4*3 = 68 2 Yes
univ[1][5] 16+4*5 = 36 9 No
univ[2][-1] 56+4*-1 = 52 5 No
univ[3][-1] ?? ?? No
univ[1][12] 16+4*12 = 64 7 No
 Code does not do any bounds checking
 Location of each lower-level array in memory is not guaranteed
Multi-level Arrays
University of Washington

Arrays in C
 Contiguous allocations of memory
 No bounds checking
 Can usually be treated like a pointer to first element
(elements are offset from start of array)
 Nested (multi-dimensional) arrays are contiguous in memory
(row-major order)
 Multi-level arrays are not contiguous
(pointers used between levels)

Multi-level Arrays

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy