Structs Multidimensional arrays Alignment CS 295 Structs Alignment

  • Slides: 38
Download presentation
Structs, Multi-dimensional arrays & Alignment CS 295 Structs & Alignment Acknowledgments: These slides have

Structs, Multi-dimensional arrays & Alignment CS 295 Structs & Alignment Acknowledgments: These slides have been modified by Arrvindh Shriraman, Justi

Structs, Multi-dimensional arrays & Alignment CS 295 Data Structures in Assembly v v Arrays

Structs, Multi-dimensional arrays & Alignment CS 295 Data Structures in Assembly v v Arrays § One-dimensional § Multi-dimensional (nested) § Multi-level Structs § Alignment 2

Structs, Multi-dimensional arrays & Alignment CS 295 Array Allocation v char msg[12]; x x

Structs, Multi-dimensional arrays & Alignment CS 295 Array Allocation v char msg[12]; x x + 12 int val[5]; x x+4 x+8 x + 12 x + 16 x + 20 double a[3]; x x+8 x + 16 x + 24 char* p[3]; (or char *p[3]; ) x + 16 x + 24 3

Structs, Multi-dimensional arrays & Alignment CS 295 Array Access v 3 int x[5]; a

Structs, Multi-dimensional arrays & Alignment CS 295 Array Access v 3 int x[5]; a 7 a+4 1 a+8 9 a+12 5 a+16 a+20 x[4] int 5 x int* a x+1 int* a + 4 &x[2] int* a + 8 x[5] int ? ? (whatever’s in memory at addr x+20) *(x+1) int 7 x+i int* a + 4*i 4

Structs, Multi-dimensional arrays & Alignment CS 295 Array Example typedef int zip_dig[5]; zip_dig cmu

Structs, Multi-dimensional arrays & Alignment CS 295 Array Example typedef int zip_dig[5]; zip_dig cmu = { 1, 5, 2, 1, 3 }; zip_dig sfu = { 9, 8, 1, 9, 5 }; zip_dig ucb = { 9, 4, 7, 2, 0 }; v initialization typedef: Declaration “zip_dig sfu” equivalent to “int sfu[5]” 5

Structs, Multi-dimensional arrays & Alignment CS 295 C Details: Arrays and Pointers v Arrays

Structs, Multi-dimensional arrays & Alignment CS 295 C Details: Arrays and Pointers v Arrays are (almost) identical to pointers § char *string and char string[] are nearly identical declarations § Differ in subtle ways: initialization, sizeof(), etc. v v An array name looks like a pointer to the first (0 th) element § ar[0] same as *ar; ar[2] same as *(ar+2) An array name is read-only (no assignment) § Cannot use "ar = <anything>" 7

Structs, Multi-dimensional arrays & Alignment CS 295 C Details: Arrays and Functions v Declared

Structs, Multi-dimensional arrays & Alignment CS 295 C Details: Arrays and Functions v Declared arrays only allocated while the scope is valid: char* foo() { char string[32]; . . . ; return string; } v BAD! An array is passed to a function as a pointer: § Array size gets lost! int foo(int ar[], unsigned Really intint *ar size) {. . . ar[size-1]. . . } Must explicitly pass the size! 8

Structs, Multi-dimensional arrays & Alignment typedef int zip_dig[5]; Referencing Examples 1 zip_dig cmu; 16

Structs, Multi-dimensional arrays & Alignment typedef int zip_dig[5]; Referencing Examples 1 zip_dig cmu; 16 20 9 zip_dig sfu; 36 v 24 40 56 1 4 Reference Address sfu[3] sfu[6] sfu[-1] cmu[15] 36 36 36 16 4* 3 4* 6 4*-1 4*15 48 60 32 76 3 32 9 48 7 64 = = 1 28 44 60 + + 2 8 9 zip_dig ucb; v 5 CS 295 36 5 52 2 68 56 0 72 76 Value Guaranteed? 9 4 3 ? ? Yes No No bounds checking Example arrays happened to be allocated in successive 20 byte blocks § Not guaranteed to happen in general 9

Structs, Multi-dimensional arrays & Alignment Nested Array Example zip_dig {{ 9, sea[4] = 8,

Structs, Multi-dimensional arrays & Alignment Nested Array Example zip_dig {{ 9, sea[4] = 8, 1, 9, 8, 1, 0, 8, 1, 1, 5 5 3 5 same as: int sea[4][5]; }, }, }, }}; CS 295 typedef int zip_dig[5]; Remember, T A[N] is an array with elements of type T, with length N What is the layout in memory? 10

Structs, Multi-dimensional arrays & Alignment typedef int zip_dig[5]; Nested Array Example zip_dig {{ 9,

Structs, Multi-dimensional arrays & Alignment typedef int zip_dig[5]; Nested Array Example zip_dig {{ 9, sea[4] = 8, 1, 9, 8, 1, 0, 8, 1, 1, 5 5 3 5 Remember, T A[N] is an array with elements of type T, with length N }, }, }, }}; Row 0 CS 295 sea[3][2]; Row 2 Row 1 Row 3 9 8 1 9 5 9 8 1 0 3 9 8 1 1 5 76 v v v 96 116 136 “Row-major” ordering of all elements Elements in the same row are contiguous Guaranteed (in C) 156 11

Structs, Multi-dimensional arrays & Alignment CS 295 Two-Dimensional (Nested) Arrays v Declaration: T A[R][C];

Structs, Multi-dimensional arrays & Alignment CS 295 Two-Dimensional (Nested) Arrays v Declaration: T A[R][C]; § 2 D array of data type T § R rows, C columns § Each element requires sizeof(T) bytes v A[0][0] • • • A[0][C-1] • • • A[R-1][0] • • • A[R-1][C-1] Array size? 12

Structs, Multi-dimensional arrays & Alignment CS 295 Two-Dimensional (Nested) Arrays v Declaration: T A[R][C];

Structs, Multi-dimensional arrays & Alignment CS 295 Two-Dimensional (Nested) Arrays v Declaration: T A[R][C]; § 2 D array of data type T § R rows, C columns § Each element requires sizeof(T) bytes v v A[0][0] • • • A[0][C-1] • • • A[R-1][0] • • • A[R-1][C-1] Array size: § R*C*sizeof(T) bytes Arrangement: row-major ordering int A[R][C]; A [0] A A • • • [0] [1] [C-1] [0] A • • • [1] [C-1] 4*R*C bytes • • • A A [R-1] • • • [R-1] [0] [C-1] 13

Structs, Multi-dimensional arrays & Alignment CS 295 Multi-Level Array Example Multi-Level Array Declaration(s): int

Structs, Multi-dimensional arrays & Alignment CS 295 Multi-Level Array Example Multi-Level Array Declaration(s): int cmu[5] = { 1, 5, 2, 1, 3 }; int sfu[5] = { 9, 8, 1, 9, 5 }; int ucb[5] = { 9, 4, 7, 2, 0 }; int* univ[3] = {sfu, cmu, ucb}; 2 D Array Declaration: zip_dig univ 2 D[3] { 9, 8, 1, 9, 5 { 1, 5, 2, 1, 3 { 9, 4, 7, 2, 0 }; Is a multi-level array the NO same thing as a 2 D array? = { }, }, } One array declaration = one contiguous block of memory 14

Structs, Multi-dimensional arrays & Alignment CS 295 Array Element Accesses Nested array Multi-level array

Structs, Multi-dimensional arrays & Alignment CS 295 Array Element Accesses Nested array Multi-level array int get_sea_digit (int index, int digit) { return sea[index][digit]; } int get_univ_digit (int index, int digit) { return univ[index][digit]; } cmu univ 160 36 168 16 176 60 sfu ucb 1 16 5 20 9 36 24 8 40 9 60 2 28 1 44 4 64 1 32 9 48 7 68 3 5 52 2 72 36 56 0 76 80 Access looks the same, but it isn’t: Mem[sea+20*index+4*digit] Mem[univ+8*index]+4*digit] 15

Structs, Multi-dimensional arrays & Alignment CS 295 Multi-Level Referencing Examples cmu univ 160 36

Structs, Multi-dimensional arrays & Alignment CS 295 Multi-Level Referencing Examples cmu univ 160 36 168 16 176 60 sfu 1 16 20 9 ucb 36 Address 2 24 8 40 9 60 Reference 5 Value 28 1 44 4 64 1 32 9 48 7 68 3 5 52 2 72 36 56 0 76 80 Guaranteed? univ[2][3] univ[1][5] univ[2][-2] univ[3][-1] univ[1][12] § C code does not do any bounds checking § Location of each lower-level array in memory is not guaranteed 16

Structs, Multi-dimensional arrays & Alignment CS 295 Summary v 17

Structs, Multi-dimensional arrays & Alignment CS 295 Summary v 17

Structs, Multi-dimensional arrays & Alignment CS 295 Data Structures in Assembly v v v

Structs, Multi-dimensional arrays & Alignment CS 295 Data Structures in Assembly v v v Arrays § One-dimensional § Multi-dimensional (nested) § Multi-level Structs § Alignment Unions 18

Structs, Multi-dimensional arrays & Alignment CS 295 Structs in C v v Way of

Structs, Multi-dimensional arrays & Alignment CS 295 Structs in C v v Way of defining compound data types A structured group of variables, possibly including other structs typedef struct { int length. In. Seconds; int year. Recorded; } Song; Song song 1; song 1. length. In. Seconds = 213; song 1. year. Recorded = 1994; Song song 2; song 2. length. In. Seconds = 248; song 2. year. Recorded = 1988; 19

Structs, Multi-dimensional arrays & Alignment CS 295 Accessing Structure Members v Given a struct

Structs, Multi-dimensional arrays & Alignment CS 295 Accessing Structure Members v Given a struct instance, access member using the. operator: struct rec r 1; r 1. i = val; v Given a pointer to a struct: struct rec { int a[4]; long i; struct rec *next; }; struct rec *r; r = &r 1; // or malloc space for r to point to We have two options: • Use * and. operators: (*r). i = val; • Use -> operator for short: r->i = val; v In assembly: register holds address of the first byte § Access members with offsets 20

Structs, Multi-dimensional arrays & Alignment CS 295 Structure Representation struct rec { int a[4];

Structs, Multi-dimensional arrays & Alignment CS 295 Structure Representation struct rec { int a[4]; long i; struct rec *next; }; r i a 0 16 next 24 32 struct rec *r; v Characteristics § Contiguously-allocated region of memory § Refer to members within structure by names § Members may be of different types 21

Structs, Multi-dimensional arrays & Alignment CS 295 Structure Representation struct rec { int a[4];

Structs, Multi-dimensional arrays & Alignment CS 295 Structure Representation struct rec { int a[4]; long i; struct rec *next; }; r i a 0 16 next 24 32 struct rec *r; v v v Structure represented as block of memory § Big enough to hold all of the fields Fields ordered according to declaration order § Even if another ordering would be more compact Compiler determines overall size + positions of fields § Machine-level program has no understanding of the structures in the source code 22

Structs, Multi-dimensional arrays & Alignment CS 295 Accessing a Structure Member struct rec {

Structs, Multi-dimensional arrays & Alignment CS 295 Accessing a Structure Member struct rec { int a[4]; long i; struct rec *next; }; r 0 Compiler knows the offset of each member within a struct § Compute as add *(r+offset) • i a struct rec *r; v r->i 16 next 24 32 long get_i(struct rec *r) { return r->i; } a 0, a 1, 16 # Coming up in Week 3 ret Referring to absolute offset, so no pointer arithmetic 23

Structs, Multi-dimensional arrays & Alignment CS 295 Generating Pointer to Array Element struct rec

Structs, Multi-dimensional arrays & Alignment CS 295 Generating Pointer to Array Element struct rec { int a[4]; long i; struct rec *next; }; r r+4*index i a 0 16 next 24 32 struct rec *r; v Generating Pointer to Array Element § Offset of each structure member determined at compile time § Compute as: r+4*index int* find_addr_of_array_elem (struct rec *r, long index) { return &r->a[index]; } &(r->a[index]) 24

Structs, Multi-dimensional arrays & Alignment CS 295 Struct Definitions v Structure definition: § Does

Structs, Multi-dimensional arrays & Alignment CS 295 Struct Definitions v Structure definition: § Does NOT declare a variable § Variable type is “struct name” struct name { /* fields */ }; pointer Easy to forget semicolon! struct name 1, *pn, name_ar[3]; array v Joint struct definition and typedef § Don’t need to give struct a name in this case struct nm { /* fields */ }; typedef struct nm name; name n 1; typedef struct { /* fields */ } name; name n 1;

Structs, Multi-dimensional arrays & Alignment CS 295 Scope of Struct Definition v Why is

Structs, Multi-dimensional arrays & Alignment CS 295 Scope of Struct Definition v Why is placement of struct definition important? § What actually happens when you declare a variable? • Creating space for it somewhere! § Without definition, program doesn’t know how much space struct data { int ar[4]; long d; }; v Size = _____ bytes struct rec { int a[4]; long i; struct rec* next; }; Almost always define structs in global scope near the top of your C file § Struct definitions follow normal rules of scope 26

Structs, Multi-dimensional arrays & Alignment CS 295 Nested Struct struct foo { long a;

Structs, Multi-dimensional arrays & Alignment CS 295 Nested Struct struct foo { long a; long b; struct bar my_bar; }; struct bar { long x; long y; }; &f->my_bar. y a 0 b 8 x 16 y 24 32 struct foo *f; 27

Structs, Multi-dimensional arrays & Alignment CS 295 Nested Struct struct foo { long a;

Structs, Multi-dimensional arrays & Alignment CS 295 Nested Struct struct foo { long a; long b; struct foo my_foo; }; a 0 b 8 ? ? ? ? ? 16 28

Structs, Multi-dimensional arrays & Alignment CS 295 Review: Memory Alignment v Type Addresses 1

Structs, Multi-dimensional arrays & Alignment CS 295 Review: Memory Alignment v Type Addresses 1 char No restrictions 2 short Lowest bit must be zero: … 02 4 int, float Lowest 2 bits zero: … 002 8 long, double, * Lowest 3 bits zero: … 0002 29

Structs, Multi-dimensional arrays & Alignment CS 295 Alignment Principles v 30

Structs, Multi-dimensional arrays & Alignment CS 295 Alignment Principles v 30

Structs, Multi-dimensional arrays & Alignment CS 295 Structures & Alignment v c i[0] p

Structs, Multi-dimensional arrays & Alignment CS 295 Structures & Alignment v c i[0] p p+1 i[1] p+5 c 3 bytes p+0 p+4 v p+9 i[0] i[1] p+8 p+17 4 bytes struct S 1 { char c; int i[2]; double v; } *p; v p+16 Multiple of 4 Multiple of 8 internal fragmentation p+24 Multiple of 8 31

Structs, Multi-dimensional arrays & Alignment CS 295 Satisfying Alignment with Structures (1) struct S

Structs, Multi-dimensional arrays & Alignment CS 295 Satisfying Alignment with Structures (1) struct S 1 { char c; int i[2]; double v; } *p; v c 3 bytes p+0 p+4 i[0] i[1] p+8 4 bytes v p+16 Multiple of 4 Multiple of 8 internal fragmentation p+24 32

Structs, Multi-dimensional arrays & Alignment CS 295 Satisfying Alignment with Structures (2) struct S

Structs, Multi-dimensional arrays & Alignment CS 295 Satisfying Alignment with Structures (2) struct S 2 { double v; int i[2]; char c; } *p; v v p+0 Multiple of 8 i[0] p+8 i[1] c p+16 external fragmentation 7 bytes p+24 Multiple of 8 33

Structs, Multi-dimensional arrays & Alignment CS 295 Arrays of Structures struct S 2 {

Structs, Multi-dimensional arrays & Alignment CS 295 Arrays of Structures struct S 2 { double v; int i[2]; char c; } a[10]; v a[0] a+0 a[1] a+24 v a+24 i[0] a+32 • • • a[2] a+48 i[1] a+72 c a+40 external fragmentation 7 bytes a+48 34

Structs, Multi-dimensional arrays & Alignment CS 295 Accessing Array Elements Compute start of array

Structs, Multi-dimensional arrays & Alignment CS 295 Accessing Array Elements Compute start of array element as: 12*index v struct S 3 { short i; float v; short j; } a[10]; § sizeof(S 3) = 12, including alignment padding Element j is at offset 8 within structure Assembler gives offset a+8 v v • • • a[0] a+0 a+12 • • • a[index] a+12*index i 2 bytes v j 2 bytes a+12*index+8 short get_j(int index) { return a[index]. j; } 35

Structs, Multi-dimensional arrays & Alignment CS 295 Alignment of Structs v Compiler will do

Structs, Multi-dimensional arrays & Alignment CS 295 Alignment of Structs v Compiler will do the following: § Maintains declared ordering of fields in struct § Each field must be aligned within the struct (may insert padding) • offsetof can be used to get actual field offset § Overall struct must be aligned according to largest field § Total struct size must be multiple of its alignment (may insert padding) • sizeof should be used to get true size of structs 36

Structs, Multi-dimensional arrays & Alignment CS 295 How the Programmer Can Save Space v

Structs, Multi-dimensional arrays & Alignment CS 295 How the Programmer Can Save Space v Compiler must respect order elements are declared in § Sometimes the programmer can save space by declaring large data types first struct S 4 { char c; int i; char d; } *p; c 3 bytes i 12 bytes d struct S 5 { int i; char c; char d; } *p; 3 bytes i c d 2 bytes 8 bytes 37

Structs, Multi-dimensional arrays & Alignment CS 295 Peer Instruction Question v Minimize the size

Structs, Multi-dimensional arrays & Alignment CS 295 Peer Instruction Question v Minimize the size of the struct by re-ordering the vars struct old { int i; short s[3]; ______; char *c; ______; float f; ______; }; v struct new { int i; }; What are the old and new sizes of the struct? sizeof(struct old) = _____ sizeof(struct new) = _____ 38

Structs, Multi-dimensional arrays & Alignment CS 295 Summary v v Arrays in C §

Structs, Multi-dimensional arrays & Alignment CS 295 Summary v v Arrays in C § Aligned to satisfy every element’s alignment requirement Structures § Allocate bytes in order declared § Pad in middle and at end to satisfy alignment 39