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

array layout
SMART_READER_LITE
LIVE PREVIEW

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 +


slide-1
SLIDE 1

11/5/15 1

More ¡data ¡layout! Array ¡Layout

Array ¡ of ¡data ¡type ¡T ¡and ¡length ¡N Contiguously allocated ¡memory ¡ region ¡of ¡N ¡* ¡(size ¡of ¡T ¡in ¡bytes)

5 char a[12] x x ¡+ ¡12 int b[5] x x ¡+ ¡4 x ¡+ ¡8 x ¡+ ¡12 x ¡+ ¡16 x ¡+ ¡20 double c[3]

x ¡+ ¡24

x x ¡+ ¡8 x ¡+ ¡16 short* d[3] x x ¡+ ¡4 x ¡+ ¡8 x ¡+ ¡12

Two-­‑Dimensional ¡Arrays

Declaration

2D ¡array ¡of ¡data ¡type ¡T R ¡rows, ¡C ¡columns Type ¡T ¡element ¡requires ¡K ¡bytes

Array ¡ layout ¡ and ¡size?

6

A[0][0] A[0][C-1] A[R-1][0]

  • • •
  • • • A[R-1][C-1]
  • Nested ¡Arrays (C)

Declaration

2D ¡array ¡of ¡data ¡type ¡T R ¡rows, ¡C ¡columns Type ¡T ¡element ¡requires ¡K ¡bytes

Layout

Row-­‑major ¡ordering

7

int A[R][C];

  • • •

A [0] [0] A [0] [C-1]

  • • •

A [1] [0] A [1] [C-1]

  • • •

A [R-1] [0] A [R-1] [C-1]

  • A[0][0]

A[0][C-1] A[R-1][0]

  • • •
  • • • A[R-1][C-1]
slide-2
SLIDE 2

11/5/15 2

Uh, ¡raise ¡awareness ¡day!

Seat ¡= ¡4 ¡bytes Array ¡ tag! Call ¡out ¡2D ¡index. Stand ¡up ¡if ¡that's ¡you. Nested ¡arrays: ¡ 4 ¡rows ¡x ¡8 ¡columns. ¡ ¡Element ¡size: ¡4 ¡bytes.

Nested ¡Array ¡Access (C)

Each ¡row ¡is ¡an ¡array.

T ¡ ¡ ¡A[R][C]: ¡ ¡A[i] ¡is ¡array ¡of ¡C ¡elements Each ¡element ¡of ¡type ¡T ¡requires ¡K ¡bytes Starting ¡ address: ¡____________________________

Array ¡Elements ¡

A[i][j] ¡is ¡element ¡of ¡type ¡T, ¡which ¡requires ¡K ¡bytes Address ¡of ¡A[i][j]: ¡____________________________

  • ¡ • •
  • • • ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡• • •

A [i] [j] A[i]

  • • •

A [R-1] [0] A [R-1] [C-1] A[R-1]

  • ¡ • •

A

  • • •

A [0] [0] A [0] [C-1] A[0]

int A[R][C];

Strange ¡Referencing ¡Examples (C)

Reference Address Value Guaranteed?

sea[3][3] sea[2][5] sea[2][-1] sea[4][-1] sea[0][19] sea[0][-1]

C ¡does ¡not ¡do ¡any ¡bounds ¡checking. Row-­‑major ¡ array ¡ layout ¡ is ¡guaranteed.

10

int sea[4][5]; 76 96 116 136 156 9 8 1 9 5 9 8 1 0 5 9 8 1 0 3 9 8 1 1 5 76+20*3+4*3 = 148 1 Yes 76+20*2+4*5 = 136 9 Yes 76+20*2+4*-1 = 112 5 Yes 76+20*4+4*-1 = 152 5 Yes 76+20*0+4*19 = 152 5 Yes 76+20*0+4*-1 = 72 ?? No

Multi-­‑Level ¡Arrays (Java, ¡C)

13

36 160 164 168 zips 2 4 8 1 36 40 44 48 52 56 int[][] zips = new int[3][]; zips[0] = new int[5] {0, 2, 4, 8, 1}; Java 3 ... 156 152 160 ... 5 32 28 Write ¡x86 ¡code ¡to ¡implement: zips[i][j] = zips[i][j - 1]; Assume: ¡zips ¡= ¡%eax, ¡i = ¡%ecx, ¡j ¡= ¡%edx [Later] ¡ How ¡would ¡you ¡implement: null ¡checking ¡and ¡bounds ¡checking. array ¡ length

  • bject ¡info

(later) int[][] int[] null pointers/references int* zips[3]; zips[0] = (int*)malloc(sizeof(int)*5); ... C

slide-3
SLIDE 3

11/5/15 3

Uh, ¡raise ¡awareness ¡day!

Seat ¡= ¡4 ¡bytes Array ¡ tag! Call ¡out ¡2D ¡index. Stand ¡up ¡if ¡that's ¡you. Nested ¡arrays: ¡ 4 ¡rows ¡x ¡8 ¡columns. Arrays ¡ of ¡addresses ¡of ¡arrays: 4 ¡rows ¡x ¡8 ¡columns Variable ¡ width Please ¡return ¡the ¡addresses ¡at ¡the ¡end.

struct rec { int i; int a[3]; int* p; };

Structures ¡in ¡C

Characteristics

Contiguously-­‑allocated ¡region ¡of ¡memory Refer ¡to ¡members ¡within ¡structure ¡by ¡names Members ¡may ¡be ¡of ¡different ¡types

Like ¡primitive ¡ version ¡of ¡Java ¡ objects

17

Memory ¡ Layout

i a p 4 16 20

struct rec { int i; int a[3]; int* p; }; void set_i(struct rec* r, int val) { r->i = val; }

Structures ¡in ¡C

18

Accessing ¡Structure ¡ Member

Given ¡a ¡struct, ¡use ¡the ¡.operator, ¡just ¡like ¡Java: struct rec r1; r1.i = val; Given ¡a ¡pointer to ¡a ¡struct, ¡use ¡*and ¡.operators ¡or -> shorthand: ¡ struct rec* r = &r1; (*r).i = val; r->i = val;

equivalent

}

struct assignment

struct rec a; struct rec b; a.i = 1; a.a[0] = 2; a.p = &(a.i); b = a; struct rec* c; c = &b; (*c).i++; Copies ¡entire ¡contents ¡of ¡a ¡into ¡b. No ¡copying What ¡is ¡b.i? ¡ ¡a.i?

slide-4
SLIDE 4

11/5/15 4

typedef

struct rec ¡{ ¡... ¡}; struct rec ¡a; typedef struct list_node { ... } ¡list_node;

struct rec { int i; int a[3]; int* p; };

IA32 ¡Assembly

# %eax = val # %edx = r movl %eax,0(%edx) # Mem[r+0] = val void set_i(struct rec* r, int val) { r->i = val; }

Structures ¡in ¡C

21

Accessing ¡Structure ¡ Member

Given ¡an ¡instance ¡of ¡the ¡struct, ¡we ¡can ¡use the ¡. operator, ¡just ¡like ¡Java: struct rec r1; r1.i = val; What ¡if ¡we ¡have ¡a ¡pointer to ¡a ¡struct: ¡ ¡struct rec* r = &r1; Using ¡* and ¡. operators: Or, ¡use ¡-> operator ¡for ¡short: ¡ Pointer ¡indicates ¡first ¡byte ¡of ¡structure; ¡access ¡members ¡with ¡offsets (*r).i = val; r->i = val;

int* find_address_of_elem (struct rec* r, int idx) { return &r->a[idx]; }

Generating ¡Pointer ¡to ¡Structure ¡Member

Offset ¡of ¡each ¡structure member ¡determined at ¡compile ¡time

22

struct rec { int i; int a[3]; int* p; };

i a p 4 16 20

r+4+4*idx r &(r->a[idx]) # %ecx = idx # %edx = r leal 4(%edx,%ecx,4),%eax # r+4*idx+4 OR # %ecx = idx # %edx = r leal 0(,%ecx,4),%eax # 4*idx leal 4(%eax,%edx),%eax # r+4*idx+4 # %ecx = idx # %edx = r leal 4(%edx,%ecx,4),%eax # r+4*idx+4 int* find_address_of_elem (struct rec* r, int idx) { return &r->a[idx]; }

Generating ¡Pointer ¡to ¡Structure ¡Member

Generating ¡Pointer ¡to Array ¡ Element

Offset ¡of ¡each ¡structure member ¡determined at ¡compile ¡time

23

struct rec { int i; int a[3]; int* p; };

i a p 4 16 20

r+4+4*idx r &(r->a[idx]) OR

slide-5
SLIDE 5

11/5/15 5

# %ecx = idx # %edx = r movl 4(%edx,%ecx,4),%eax # Mem[r+4*idx+4] int* find_address_of_elem (struct rec* r, int idx) { return &r->a[idx]; }

Accessing ¡Structure ¡Member

Reading ¡Array ¡ Element

Offset ¡of ¡each ¡structure member ¡still determined at ¡compile ¡time

24

struct rec { int i; int a[3]; int* p; };

i a p 4 16 20

r+4+4*idx r p+0 p+8

Structures ¡& ¡Alignment

Unaligned ¡ Data Aligned ¡Data

Primitive ¡data ¡type ¡requires ¡K bytes Address ¡must ¡be ¡multiple ¡of ¡K c i v

7 ¡bytes

p+16 p+20 Multiple ¡of ¡4 Multiple ¡of ¡8

25

c i v

p p+1 p+9 p+13 struct S1 { char c; double v; int i; } * p;

internal ¡fragmentation

Alignment ¡Principles

Aligned ¡Data

Primitive ¡data ¡type ¡requires ¡K ¡bytes Address ¡must ¡be ¡multiple ¡of ¡K

Aligned ¡data ¡ is ¡required ¡on ¡some ¡machines; ¡it ¡is ¡advised

  • n ¡IA32

Treated ¡differently ¡by ¡IA32 ¡Linux, ¡x86-­‑64 ¡Linux, ¡Windows, ¡Mac ¡OS ¡X, ¡…

Motivation ¡ for ¡Aligning ¡ Data

Physical ¡memory ¡is ¡accessed ¡by ¡aligned ¡chunks ¡of ¡4 ¡or ¡8 ¡bytes ¡(system-­‑ dependent) Inefficient ¡to ¡load ¡or ¡store ¡datum ¡that ¡spans ¡these ¡boundaries 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

Specific ¡Cases ¡of ¡Alignment ¡(IA32)

1 ¡byte: ¡char, ¡…

no ¡restrictions ¡on ¡address

2 ¡bytes: ¡short, ¡…

lowest ¡1 ¡bit ¡of ¡address ¡must ¡be ¡02

4 ¡bytes: ¡int, ¡float, ¡char ¡*, ¡…

lowest ¡2 ¡bits ¡of ¡address ¡must ¡be ¡002

8 ¡bytes: ¡double, ¡…

Windows ¡(and ¡most ¡other ¡OSs & ¡instruction ¡sets): ¡lowest ¡3 ¡bits ¡0002 Linux: ¡lowest ¡2 ¡bits ¡of ¡address ¡must ¡be ¡002 i.e., ¡treated ¡liked ¡2 ¡contiguous ¡4-­‑byte ¡primitive ¡data ¡items

27

slide-6
SLIDE 6

11/5/15 6

Saving ¡Space

Put ¡large ¡ data ¡types ¡first: Effect ¡(example ¡ ia32, ¡both ¡have ¡K=8)

28

p+0 p+8

c i v

7 ¡bytes

p+16 p+20 struct S1 { char c; double v; int i; } * p; struct S2 { double v; int i; char c; } * q; q+0

i v

q+8 q+12

c

But ¡actually…

q+13

Struct Alignment ¡Principles

Size ¡must ¡be ¡a ¡multiple ¡ of ¡the ¡largest ¡ primitive ¡ type ¡inside.

29

p+0 p+8

c i v

7 ¡bytes

p+16 p+20 q+0

i v

q+8 q+12

c

3 ¡bytes

q+16 K = ¡8 ¡ ¡ ¡ ¡ ¡so ¡ ¡ ¡ ¡size mod ¡8 ¡= ¡0

Arrays ¡of ¡Structures

Satisfy ¡ alignment ¡ requirement ¡ for ¡every ¡element How ¡would ¡accessing ¡an ¡element ¡work?

30

struct S2 { double v; int i; char c; } a[10]; a+16 a+24 a+28

a[0]

a+0

a[1] a[2]

a+16 a+32 a+48

  • • •

a+32

i v c

3 ¡bytes

external ¡fragmentation