260 likes | 278 Vues
Computer Systems. New to C?. Contents. Control Flow If-then-else Varieties of Loops Switch Statements Data Structures Arrays Structs Unions Putting it Together: Understanding Pointers. Basic Data Types. Integer Stored & operated on in general registers
E N D
Computer Systems New to C? Computer Systems – Data in C
Contents • Control Flow • If-then-else • Varieties of Loops • Switch Statements • Data Structures • Arrays • Structs • Unions Putting it Together: Understanding Pointers Computer Systems – Data in C
Basic Data Types • Integer • Stored & operated on in general registers • Signed vs. unsigned depends on instructions used Intel GAS Bytes C byte b 1 [unsigned] char word w 2 [unsigned] short double word l 4 [unsigned] int • Floating Point • Stored & operated on in floating point registers Intel GAS Bytes C Single s 4 float Double l 8 double Extended t 10/12 long double Computer Systems – Data in C
char string[12]; x x + 12 int val[5]; double a[4]; x x + 4 x + 8 x + 12 x + 16 x + 20 x x + 8 x + 16 x + 24 x + 32 x x + 4 x + 8 Array Allocation • Basic Principle TA[L]; • Array of data type T and length L • Contiguously allocated region of L*sizeof(T) bytes char *p[3]; Computer Systems – Data in C
int val[5]; 1 5 2 1 3 x x + 4 x + 8 x + 12 x + 16 x + 20 Array Access • Identifier A can be used as a pointer to array element 0 • Reference Type Value val[4] int 3 val int * x val+1int * x + 4 &val[2]int * x + 8 val[5]int ?? *(val+1)int 5 val + iint * x + 4 i Computer Systems – Data in C
Array Accessing Example • Computation • Register %edx contains starting address of array • Register %eax contains array index • Desired digit at 4*%eax + %edx • Use memory reference (%edx,%eax,4) int get_digit (zip_dig z, int dig) { return z[dig]; } # %edx = z # %eax = dig movl (%edx,%eax,4),%eax # z[dig] Computer Systems – Data in C
zip_dig mit; zip_dig ucb; zip_dig cmu; 9 1 0 4 5 2 2 7 1 2 3 1 3 0 9 36 16 56 40 60 20 44 24 64 48 68 28 32 72 52 76 36 56 Array Example typedef int zip_dig[5]; zip_dig cmu = { 1, 5, 2, 1, 3 }; zip_dig mit = { 0, 2, 1, 3, 9 }; zip_dig ucb = { 9, 4, 7, 2, 0 }; • Example arrays were allocated in successive 20 byte blocks • Not guaranteed to happen in general Computer Systems – Data in C
zip_dig mit; zip_dig cmu; zip_dig ucb; 0 1 9 5 2 4 7 2 1 3 2 1 0 3 9 56 36 16 40 20 60 44 24 64 28 48 68 32 52 72 76 56 36 Yes No No No Referencing Examples • Reference Address Value Guaranteed? mit[3] 36 + 4* 3 = 48 3 mit[5] 36 + 4* 5 = 56 9 mit[-1] 36 + 4*-1 = 32 3 cmu[15] 16 + 4*15 = 76 ?? Code Does Not Do Any Bounds Checking! Computer Systems – Data in C
Array Loop Example int zd2int(zip_dig z) { int i; int zi = 0; for (i = 0; i < 5; i++) { zi = 10 * zi + z[i]; } return zi; } • Original Source • Transformed Version • Eliminate loop variable i • Convert array code to pointer code • Express in do-while form • No need to test at entrance int zd2int(zip_dig z) { int zi = 0; int *zend = z + 4; do { zi = 10 * zi + *z; z++; } while(z <= zend); return zi; } Computer Systems – Data in C
zip_dig pgh[4]; 1 1 1 1 5 5 5 5 2 2 2 2 0 1 1 2 7 6 1 3 76 96 116 136 156 Nested Array Example #define PCOUNT 4 zip_dig pgh[PCOUNT] = {{1, 5, 2, 0, 6}, {1, 5, 2, 1, 3 }, {1, 5, 2, 1, 7 }, {1, 5, 2, 2, 1 }}; • Declaration “zip_dig pgh[4]” equivalent to “int pgh[4][5]” • Variable pgh denotes array of 4 elements • Allocated contiguously • Each element is an array of 5 int’s • Allocated contiguously • “Row-Major” ordering of all elements guaranteed Computer Systems – Data in C
1 1 1 1 5 5 5 5 2 2 2 2 2 1 1 0 3 7 6 1 76 96 116 136 156 Strange Referencing Examples zip_dig pgh[4]; • Reference Address Value Guaranteed? pgh[3][3] 76+20*3+4*3 = 148 2 pgh[2][5] 76+20*2+4*5 = 136 1 pgh[2][-1] 76+20*2+4*-1 = 112 3 pgh[4][-1] 76+20*4+4*-1 = 152 1 pgh[0][19] 76+20*0+4*19 = 152 1 pgh[0][-1] 76+20*0+4*-1 = 72 ?? • Code does not do any bounds checking • Ordering of elements within array guaranteed Yes Yes Yes Yes Yes No Computer Systems – Data in C
0 1 9 5 2 4 2 1 7 2 1 3 0 9 3 cmu 56 36 16 20 60 40 64 24 44 48 28 68 72 52 32 56 36 76 univ 160 36 mit 164 16 ucb 168 56 Multi-Level Array Example zip_dig cmu = { 1, 5, 2, 1, 3 }; zip_dig mit = { 0, 2, 1, 3, 9 }; zip_dig ucb = { 9, 4, 7, 2, 0 }; • Variable univ denotes array of 3 elements • Each element is a pointer • Each pointer points to array of int’s #define UCOUNT 3 int *univ[UCOUNT] = {mit, cmu, ucb}; Computer Systems – Data in C
Similar C references Nested Array Element at Mem[pgh+20*index+4*dig] Different address computation Multi-Level Array Element at Mem[Mem[univ+4*index]+4*dig] Array Element Accesses int get_pgh_digit (int index, int dig) { return pgh[index][dig]; } int get_univ_digit (int index, int dig) { return univ[index][dig]; } Computer Systems – Data in C
0 9 1 4 2 5 7 2 1 2 3 1 0 9 3 cmu 36 16 56 20 40 60 64 44 24 28 48 68 32 72 52 36 76 56 univ 160 36 mit 164 16 Yes No ucb 168 56 No No No Strange Referencing Examples • Reference Address Value Guaranteed? univ[2][3] 56+4*3 = 68 2 univ[1][5] 16+4*5 = 36 0 univ[2][-1] 56+4*-1 = 52 9 univ[3][-1] ?? ?? univ[1][12] 16+4*12 = 64 7 • Code does not do any bounds checking • Ordering of elements in different arrays not guaranteed Computer Systems – Data in C
Dynamic Nested Arrays int * new_var_matrix(int n) { return (int *) calloc(sizeof(int), n*n); } • Strength • Can create matrix of arbitrary size • Programming • Must do index computation explicitly • Performance • Must do multiplicationfor index int var_ele (int *a, int i, int j, int n) { return a[i*n+j]; } movl 12(%ebp),%eax # i movl 8(%ebp),%edx # a imull 20(%ebp),%eax # n*i addl 16(%ebp),%eax # n*i+j movl (%edx,%eax,4),%eax # Mem[a+4*(i*n+j)] Computer Systems – Data in C
i a p 20 0 4 16 Structures • Concept • Contiguously-allocated region of memory • Refer to members within structure by names • Members may be of different types • Accessing Structure Member struct rec { int i; int a[3]; int *p; }; Memory Layout Assembly void set_i(struct rec *r, int val) { r->i = val; } # %eax = val # %edx = r movl %eax,(%edx) # Mem[r] = val Computer Systems – Data in C
Alignment • Aligned Data • Primitive data type requires K bytes • Address must be multiple of K • Required on some machines; advised on IA32 • treated differently by Linux and Windows! • Compiler • Inserts gaps in structure to ensure correct alignment of fields Computer Systems – Data in C
Satisfying Alignment struct S1 { char c; int i[2]; double v; } *p; • Offsets Within Structure • Must satisfy element’s alignment requirement • Initial address & structure length must be multiples of KExample (under Windows): • K = 8, due to double element c i[0] i[1] v p+0 p+4 p+8 p+16 p+24 Multiple of 8 Multiple of 8 Multiple of 4 Multiple of 8 Computer Systems – Data in C
x i[0] i[1] c p+0 p+8 p+12 p+16 Windows: p+24 Linux: p+20 Overall Alignment Requirement struct S2 { double x; int i[2]; char c; } *p; p must be multiple of: 8 for Windows 4 for Linux Computer Systems – Data in C
a[1].i a[1].v a[1].j • • • a[0] a[1] a[2] a+0 a+12 a+24 a+36 Arrays of Structures struct S6 { short i; float v; short j; } a[10]; • Principle • Allocated by repeating allocation for array type • In general, may nest arrays & structures to arbitrary depth a+12 a+16 a+20 a+24 Computer Systems – Data in C
c i[0] i[1] c i[0] i[1] v v up+0 up+4 up+8 sp+0 sp+4 sp+8 sp+16 sp+24 Union Allocation. • Principles • Overlay union elements • Allocate according to largest element • Can only use one field at a time union U1 { char c; int i[2]; double v; } *up; struct S1 { char c; int i[2]; double v; } *sp; (Windows alignment) Computer Systems – Data in C
Summary • Arrays in C • Contiguous allocation of memory • Pointer to first element • No bounds checking • Structures • Allocate bytes in order declared • Pad in middle and at end to satisfy alignment • Unions • Overlay declarations Computer Systems – Data in C
Assignment • Practice Problem 3.23 • Read: 3.11 Putting it Together: Understanding Pointers • Study:Figure 3.26 Computer Systems – Data in C
Question int *p[13] int *(p[13]) int (*p)[13] Computer Systems – Data in C
C pointer declarations int *p p is a pointer to int int *p[13] p is an array[13] of pointer to int int *(p[13]) p is an array[13] of pointer to int int **p p is a pointer to a pointer to an int int (*p)[13] p is a pointer to an array[13] of int int *f() f is a function returning a pointer to int int (*f)() f is a pointer to a function returning int int (*(*f())[13])() f is a function returning ptr to an array[13] of pointers to functions returning int int (*(*x[3])())[5] x is an array[3] of pointers to functions returning pointers to array[5] of ints Computer Systems – Data in C