Univ23 5643 68 2 univ15 1645 36 9 univ2 1

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: level Arrays University of Washington Element Access in MulM ­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]   ComputaMon (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 MulM ­level Arrays University of Washington Array Element Accesses Nested array int get_sea_digit (int index, int dig) { return sea[index][dig]; } MulM ­level array int get_univ_digit (int index, int 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] MulM ­level Arrays University of Washington Strange Referencing Examples cmu univ 160 164 16 168 uw 36 56 1 16 Reference 20 9 ucb 36 Address 2 24 8 40 9 56   5 28 1 44 4 60 Value 1 48 36 5 52 2 68 56 0 72 Guaranteed? univ[2][3] 56+4*3 = 68 2 univ[1][5] 16+4*5 = 36 9 univ[2][-1] 56+4*-1 = 52 5 univ[3][-1] ?? ?? univ[1][12] 16+4*12 = 64 7   Code does not do any bounds checking   LocaNon of each lower ­level array in memory is not guaranteed MulM ­level Arrays 32 9 7 64 3 Yes No No No No 76 University of Washington Arrays in C           ConMguous allocaMons of memory No bounds checking Can usually be treated like a pointer to first element (elements are offset from start of array) Nested (mulM ­dimensional) arrays are conMguous in memory (row ­major order) MulM ­level arrays are not conMguous (pointers used between levels) MulM ­level Arrays University of Washington Arrays & Other Data Structures           Array allocaMon and access in memory MulM ­dimensional or nested arrays MulM ­level arrays Other structures in memory Data structures and alignment Structures University of Washington Structures struct rec { int i; int a[3]; int *p; }; Structures University of Washington Structures struct rec { int i; int a[3]; int *p; };   Memory Layout ia 0 p 4 16 20 CharacterisMcs   ConNguously ­allocated region of memory   Refer to members within struc...
View Full Document

This document was uploaded on 04/04/2014.

Ask a homework question - tutors are online