array layout
play

Array Layout Array of data type T and length N Contiguously - PowerPoint PPT Presentation

11/5/15 Array Layout Array of data type T and length N Contiguously allocated memory region of N * (size of T in bytes) More data layout! char a[12] x x +


  1. 11/5/15 Array ¡Layout Array ¡ of ¡data ¡type ¡T ¡and ¡length ¡N Contiguously allocated ¡memory ¡ region ¡of ¡N ¡* ¡(size ¡of ¡T ¡in ¡bytes) More ¡data ¡layout! char a[12] x x ¡ + ¡12 int b[5] x x ¡ + ¡4 x ¡ + ¡8 x ¡ + ¡12 x ¡ + ¡16 x ¡ + ¡20 double c[3] x x ¡ + ¡8 x ¡ + ¡16 x ¡ + ¡24 short* d[3] x x ¡ + ¡4 x ¡ + ¡8 x ¡ + ¡12 5 Two-­‑Dimensional ¡Arrays Nested ¡Arrays (C) Declaration Declaration 2D ¡array ¡of ¡data ¡type ¡T 2D ¡array ¡of ¡data ¡type ¡T A[0][0] • • • A[0][C-1] A[0][0] • • • A[0][C-1] R ¡rows, ¡C ¡columns R ¡rows, ¡C ¡columns • • • • Type ¡T ¡element ¡requires ¡K ¡bytes Type ¡T ¡element ¡requires ¡K ¡bytes • • • • Array ¡ layout ¡ and ¡size? Layout • • • • Row-­‑major ¡ordering A[R-1][0] • • • A[R-1][C-1] A[R-1][0] • • • A[R-1][C-1] int A[R][C]; A A A A A A [0] • • • [0] [1] • • • [1] • • • [R-1] • • • [R-1] [0] [C-1] [0] [C-1] [0] [C-1] 6 7 1

  2. 11/5/15 Uh, ¡raise ¡awareness ¡day! Nested ¡Array ¡Access (C) Seat ¡= ¡4 ¡bytes Each ¡row ¡is ¡an ¡array. T ¡ ¡ ¡A[R][C]: ¡ ¡A[i] ¡is ¡array ¡of ¡C ¡elements Each ¡element ¡of ¡type ¡T ¡requires ¡K ¡bytes Array ¡ tag! Starting ¡ address: ¡____________________________ Call ¡out ¡2D ¡index. Array ¡Elements ¡ Stand ¡up ¡if ¡that's ¡you. A[i][j] ¡is ¡element ¡of ¡type ¡T, ¡which ¡requires ¡K ¡bytes Address ¡of ¡A[i][j]: ¡____________________________ Nested ¡arrays: ¡ 4 ¡rows ¡x ¡8 ¡columns. ¡ ¡Element ¡size: ¡4 ¡bytes. int A[R][C]; A[0] A[i] A[R-1] A A A A A • ¡ • • • ¡ • • [0] • • • [0] • • • ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡• • • [i] [R-1] • • • [R-1] [0] [C-1] [j] [0] [C-1] A Strange ¡Referencing ¡Examples (C) Multi-­‑Level ¡Arrays (Java, ¡C) pointers/references int[][] zips = new int[3][]; Java zips[0] = new int[5] {0, 2, 4, 8, 1}; int sea[4][5]; int* zips[3]; C zips[0] = (int*)malloc(sizeof(int)*5); 9 8 1 9 5 9 8 1 0 5 9 8 1 0 3 9 8 1 1 5 ... object ¡info array ¡ length 76 96 116 136 156 (later) int[] zips Reference Address Value Guaranteed? ... 5 0 2 4 8 1 160 sea[3][3] Yes 76+20*3+4*3 = 148 1 28 32 36 40 44 48 52 56 sea[2][5] Yes 76+20*2+4*5 = 136 9 sea[2][-1] Yes ... 76+20*2+4*-1 = 112 5 152 Write ¡x86 ¡code ¡to ¡implement: sea[4][-1] Yes 76+20*4+4*-1 = 152 5 3 156 sea[0][19] Yes 76+20*0+4*19 = 152 5 160 36 zips[i][j] = zips[i][j - 1]; sea[0][-1] No 76+20*0+4*-1 = 72 ?? 164 0 null Assume: ¡ zips ¡= ¡%eax, ¡i = ¡%ecx, ¡j ¡= ¡%edx 0 168 C ¡does ¡not ¡do ¡any ¡bounds ¡checking. [Later] ¡ How ¡would ¡you ¡implement: int[][] null ¡checking ¡and ¡bounds ¡checking. Row-­‑major ¡ array ¡ layout ¡ is ¡guaranteed. 10 13 2

  3. 11/5/15 Uh, ¡raise ¡awareness ¡day! Structures ¡in ¡C Seat ¡= ¡4 ¡bytes struct rec { Memory ¡ Layout int i; i a p int a[3]; Array ¡ tag! int* p; 0 4 16 20 }; Call ¡out ¡2D ¡index. Stand ¡up ¡if ¡that's ¡you. Characteristics Contiguously-­‑allocated ¡region ¡of ¡memory Refer ¡to ¡members ¡within ¡structure ¡by ¡names Nested ¡arrays: ¡ 4 ¡rows ¡x ¡8 ¡columns. Members ¡may ¡be ¡of ¡different ¡types Arrays ¡ of ¡addresses ¡of ¡arrays: 4 ¡rows ¡x ¡8 ¡columns Like ¡primitive ¡ version ¡of ¡Java ¡ objects Variable ¡ width Please ¡return ¡the ¡addresses ¡at ¡the ¡end. 17 Structures ¡in ¡C struct assignment struct rec a; struct rec { Accessing ¡Structure ¡ Member int i; struct rec b; int a[3]; Given ¡a ¡struct, ¡use ¡the ¡ . operator, ¡just ¡like ¡Java: int* p; a.i = 1; struct rec r1; }; a.a[0] = 2; r1.i = val; a.p = &(a.i); Given ¡a ¡ pointer to ¡a ¡struct, ¡use ¡ * and ¡ . operators ¡or -> shorthand: ¡ struct rec* r = &r1; b = a; Copies ¡entire ¡contents ¡of ¡a ¡into ¡b. } (*r).i = val; equivalent r->i = val; struct rec* c; c = &b; No ¡copying (*c).i++; void set_i(struct rec* r, int val) { r->i = val; What ¡is ¡b.i? ¡ ¡a.i? } 18 3

  4. 11/5/15 typedef Structures ¡in ¡C struct rec ¡{ ¡... ¡}; struct rec { Accessing ¡Structure ¡ Member int i; int a[3]; Given ¡an ¡instance ¡of ¡the ¡struct, ¡we ¡can ¡use struct rec ¡a; int* p; the ¡ . operator, ¡just ¡like ¡Java: }; struct rec r1; r1.i = val; typedef struct list_node { What ¡if ¡we ¡have ¡a ¡ pointer to ¡a ¡struct: ¡ ¡ struct rec* r = &r1; Using ¡ * and ¡ . operators: (*r).i = val; ... Or, ¡use ¡ -> operator ¡for ¡short: ¡ r->i = val; } ¡list_node; Pointer ¡indicates ¡first ¡byte ¡of ¡structure; ¡access ¡members ¡with ¡offsets void IA32 ¡Assembly set_i(struct rec* r, # %eax = val int val) # %edx = r { movl %eax,0(%edx) # Mem[r+0] = val r->i = val; } 21 Generating ¡Pointer ¡to ¡Structure ¡Member Generating ¡Pointer ¡to ¡Structure ¡Member struct rec { struct rec { r r+4+4*idx r r+4+4*idx int i; int i; int a[3]; int a[3]; int* p; int* p; i a p i a p }; }; 0 4 16 20 0 4 16 20 Generating ¡Pointer ¡to Offset ¡of ¡each ¡structure int* find_address_of_elem int* find_address_of_elem Array ¡ Element (struct rec* r, int idx) (struct rec* r, int idx) member ¡determined { Offset ¡of ¡each ¡structure { at ¡compile ¡time return &r->a[idx]; return &r->a[idx]; member ¡determined } } at ¡compile ¡time &(r->a[idx]) &(r->a[idx]) # %ecx = idx # %ecx = idx OR # %edx = r # %edx = r leal 0(,%ecx,4),%eax # 4*idx # %ecx = idx leal 4(%edx,%ecx,4),%eax # r+4*idx+4 leal 4(%eax,%edx),%eax # r+4*idx+4 # %edx = r leal 4(%edx,%ecx,4),%eax # r+4*idx+4 OR 22 23 4

  5. 11/5/15 Accessing ¡Structure ¡Member Structures ¡& ¡Alignment Unaligned ¡ Data struct rec { struct S1 { r r+4+4*idx int i; char c; c v i int a[3]; double v; int* p; p p+1 p+9 p+13 int i; i a p }; } * p; 0 4 16 20 Aligned ¡Data Reading ¡Array ¡ Element int* find_address_of_elem Primitive ¡data ¡type ¡requires ¡ K bytes Offset ¡of ¡each ¡structure (struct rec* r, int idx) Address ¡must ¡be ¡multiple ¡of ¡ K member ¡ still determined { at ¡compile ¡time return &r->a[idx]; } c v i 7 ¡bytes p+0 p+8 p+16 p+20 # %ecx = idx # %edx = r Multiple ¡of ¡8 Multiple ¡of ¡4 movl 4(%edx,%ecx,4),%eax # Mem[ r+4*idx+4 ] internal ¡fragmentation 24 25 Alignment ¡Principles Specific ¡Cases ¡of ¡Alignment ¡(IA32) 1 ¡byte: ¡char, ¡… Aligned ¡Data no ¡restrictions ¡on ¡address Primitive ¡data ¡type ¡requires ¡K ¡bytes 2 ¡bytes: ¡short, ¡… Address ¡must ¡be ¡multiple ¡of ¡K lowest ¡1 ¡bit ¡of ¡address ¡must ¡be ¡0 2 Aligned ¡data ¡ is ¡required ¡on ¡some ¡machines; ¡it ¡is ¡ advised 4 ¡bytes: ¡int, ¡float, ¡char ¡*, ¡… on ¡IA32 lowest ¡2 ¡bits ¡of ¡address ¡must ¡be ¡00 2 Treated ¡differently ¡by ¡IA32 ¡Linux, ¡x86-­‑64 ¡Linux, ¡Windows, ¡Mac ¡OS ¡X, ¡… 8 ¡bytes: ¡double, ¡… Motivation ¡ for ¡Aligning ¡ Data Windows ¡(and ¡most ¡other ¡OSs & ¡instruction ¡sets): ¡lowest ¡3 ¡bits ¡000 2 Physical ¡memory ¡is ¡accessed ¡by ¡aligned ¡chunks ¡of ¡4 ¡or ¡8 ¡bytes ¡(system-­‑ dependent) Linux: ¡lowest ¡2 ¡bits ¡of ¡address ¡must ¡be ¡00 2 Inefficient ¡to ¡load ¡or ¡store ¡datum ¡that ¡spans ¡these ¡boundaries i.e., ¡treated ¡liked ¡2 ¡contiguous ¡4-­‑byte ¡primitive ¡data ¡items Also, ¡virtual ¡memory ¡is ¡very ¡tricky ¡when ¡datum ¡spans ¡two ¡pages ¡(later…) Compiler Inserts ¡padding ¡in ¡structure ¡to ¡ensure ¡correct ¡alignment ¡of ¡fields sizeof() should ¡be ¡used ¡to ¡get ¡true ¡size ¡of ¡structs 26 27 5

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend