148 136 112 152 152 72 1 9 5 5 5 code does

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: Nested Arrays • • • A + (R-1)*C*4 A [R-1] [C-1] University of Washington Nested Array Element Access Code int get_sea_digit (int index, int dig) { return sea[index][dig]; } zip_dig {{ 9, { 9, { 9, { 9, # %ecx = dig # %eax = index leal 0(,%ecx,4),%edx leal (%eax,%eax,4),%eax movl sea(%edx,%eax,4),%eax   sea[PCOUNT] = 8, 1, 9, 5 }, 8, 1, 0, 5 }, 8, 1, 0, 3 }, 8, 1, 1, 5 }}; # 4*dig # 5*index # *(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 Nested Arrays University of Washington Strange Referencing Examples zip_dig sea[4]; 98195981059810398115 76   96 Reference Address sea[3][3] sea[2][5] sea[2][-1] sea[4][-1] sea[0][19] sea[0][-1] 76+20*3+4*3 76+20*2+4*5 76+20*2+4*-1 76+20*4+4*-1 76+20*0+4*19 76+20*0+4*-1 116 136 156 Value Guaranteed? = = = = = = 148 136 112 152 152 72 1 9 5 5 5 ?? Code does not do any bounds checking   Ordering of elements within array guaranteed   Nested Arrays Yes Yes Yes Yes Yes No 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 MulM ­level Arrays University of Washington MulM ­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? MulM ­level Arrays University of Washington MulM ­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}; cmu univ 160 36 164 16 168 56 uw 1 16 5 20 9 ucb 36 2 24 8 40 9 56   1 28 1 44 4 60 Variable univ denotes array of 3 elements Each element is a pointer   4 bytes Each pointer points to array of ints 32 9 48 7 64 3 5 52 2 68 36 56 0 72 76 Note: this is how Java represents mulM ­dimensional arrays. MulM ...
View Full Document

This document was uploaded on 04/04/2014.

Ask a homework question - tutors are online