12 22 2016
play

12/22/2016 Structures Complex data type defined by programmer - PDF document

12/22/2016 Structures Complex data type defined by programmer Keeps together pertinent information of an object Structures Contains simple data types or other complex data types Similar to a class in C++ or Java, but without methods


  1. 12/22/2016 Structures Complex data type defined by programmer  Keeps together pertinent information of an object Structures  Contains simple data types or other complex data types  Similar to a class in C++ or Java, but without methods Example from graphics: a point has two coordinates struct point { double x; double y; };  x and y are called members of struct point Since a structure is a data type, you can declare variables: struct point p1, p2; What is the size of struct point? 16 – 2 – Accessing structures More structures struct point { Structures can contain other structures as members: double x; struct rectangle { double y; struct point pt1; }; struct point pt2; struct point p1; }; Use the “ . ” operator on structure objects to obtain members What is the size of a struct rectangle? 32 p1.x = 10; p1.y = 20; Structures can be arguments of functions Use the “ -> ” operator on structure pointers to obtain members  Passed by value like most other data types struct point *pp=&p1;  Compare to arrays double d;  Long-form for accessing structures via pointer d = (*pp).x;  Short-form using “ -> ” operator d = pp->x; Initializing structures like other variables: struct point p1 = {320, 200};  Equivalent to: p1.x = 320; p1.y = 200; – 3 – – 4 – 1

  2. 12/22/2016 More structures More structures #include <stdio.h> struct two_arrays { #include <stdio.h> char a[200]; struct two_arrays { Arrays within structures char b[200]; Avoid copying via char a[200]; }; are passed by value! pointer passing... char b[200]; void func(struct two_arrays t, long i) { }; printf("t.a is at: %p t.b is at: %p\n",&t.a,&t.b); void func(struct two_arrays *t, int i) { if (i>0) func(t,i-1); printf("t->a is at: %p t->b is at: %p\n",&t->a,&t->b); } if (i>0) func(t,i-1); main() { } struct two_arrays a; main() { func(a,2); struct two_arrays a, *ap; } ap = &a; % ./a.out func(ap,2); t.a is at: 0x7ffe77b2b8d0 t.b is at: 0x7ffe77b2b998 } t.a is at: 0x7ffe77b2b720 t.b is at: 0x7ffe77b2b7e8 % ./a.out t.a is at: 0x7ffe77b2b570 t.b is at: 0x7ffe77b2b638 t.a is at: 0x7ffdea1f79d0 t.b is at: 0x7ffdea1f7a98 % objdump -d a.out t.a is at: 0x7ffdea1f79d0 t.b is at: 0x7ffdea1f7a98 ... t.a is at: 0x7ffdea1f79d0 t.b is at: 0x7ffdea1f7a98 40061c: mov $0x32,%eax % objdump -d a.out 400621: mov %rdx,%rdi … 400624: mov %rax,%rcx 400619: mov $0x2,%esi 400627: rep movsq %ds:(%rsi),%es:(%rdi) 40061e: mov %rsp,%rdi 40062a: mov $0x2,%edi 400621: callq 4005bd <func> 40062f: callq 40059d <func> – 5 – – 6 – Operations on structures C typedef C allows us to declare new datatypes using “ typedef” keyword Legal operations  The thing being named is then a data type, rather than a variable  Copy a structure (assignment equivalent to memcpy) typedef int Length;  Get its address  Access its members Length sideA; // may be more intuitive than int sideA; Illegal operations Often used when working with structs  Compare content of structures in their entirety  Must compare individual parts typedef struct tnode { char *word; Structure operator precedences int count; Treeptr left;  “ . ” and “ -> ” higher than other operators Treeptr right; } Treenode;  *p.x is the same as *(p.x) Treenode td; // struct tnode td;  ++p->x is the same as ++(p->x) – 7 – – 8 – 2

  3. 12/22/2016 Self-referential structures Structures in assembly A structure can contain members that are pointers to Concept the same struct (i.e. nodes in linked lists)  Contiguously-allocated region of memory  Members may be of different types  Accessed statically, code generated at compile-time struct tnode { Memory Layout char *word; struct rec { int count; int i; struct tnode *next; i a p int a[3]; } p; int *p; 16 24 0 4 }; Accessing Structure Member void set_i(struct rec *r, int val) Assembly { r->i = val;} # %eax = val # %rdx = r – 9 – – 10 – movl %eax,(%rdx) # Mem[r] = val Example Practice problem 3.39 r struct rec { struct prob { How many total bytes does the structure require? int i; int *p; i a p struct { int a[3]; 24 int x; int *p; 0 4 16 What are the byte offsets of the following fields? int y; }; } s; r + 4 + 4*idx p 0 struct prob *next; }; 8 s.x int * find_a (struct rec *r, int idx) 12 s.y { 16 next return &r->a[idx]; } Consider the following C code: /* sp in %rdi */ void sp_init(struct prob *sp) sp_init: { movl 12(%rdi), %eax sp->s.y sp->s.x = ___________; movl %eax, 8(%rdi) sp->p = ___________; &(sp->s.x) leaq 8(%rdi), %rax # %rcx = idx sp->next = ___________; sp movq %rax, (%rdi) } # %rdx = r movq %rdi, 16(%rdi) Fill in the missing expressions leaq 0(,%rcx,4),%rax # 4*idx ret leaq 4(%rax,%rdx),%rax # r+4*idx+4 – 11 – – 12 – 3

  4. 12/22/2016 Aligning structures Alignment in x86-64 Data must be aligned at specific offsets in memory Aligned data required on some machines; advised on x86-64 Align so that data does not cross access boundaries and cache line boundaries If primitive data type has size K bytes, address must be multiple of K Why?  Low-level memory access done in fixed sizes at fixed offsets  char is 1 byte  Can be aligned arbitrarily  Alignment allows items to be retrieved with one access  Storing a long at 0x00  short is 2 bytes » Single memory access to retrieve value  Member must be aligned on even addresses  Storing a long at 0x04  Lowest bit of address must be 0 » Two memory accesses to retrieve value  int, float are 4 bytes  Addressing code simplified  Member must be aligned to addresses divisible by 4  Scaled index addressing mode works better with aligned  Lowest 2 bits of address must be 00 members  long, double, pointers, … are 8 bytes Compiler inserts gaps in structures to ensure correct  Member must be aligned to addresses divisible by 8 alignment of fields  Lowest 3 bits of address must be 000 – 13 – – 14 – Alignment with Structures Example struct S1 { char c; Each member must satisfy its own alignment What is K for S1? int i[2]; requirement double v;  K = 8, due to double element } *p; Overall structure must also satisfy an alignment What is the size of S1? requirement “K”  24 bytes  K = Largest alignment of any element  Initial address must be multiple of K Draw S1  Structure length must be multiple of K K = 8, due to double element  For arrays of structures c i[0] i[1] v p+0 p+4 p+8 p+16 p+24 Multiple of 4 Multiple of 8 Multiple of 8 Multiple of 8 – 15 – – 16 – 4

  5. 12/22/2016 Examples Reordering to reduce wasted space struct S2 { Draw the allocation for this structure double x; struct S4 { int i[2]; char c1; char c; 10 bytes wasted double v; } *p; char c2; int i; x i[0] i[1] c } *p; p+0 p+8 p+12 p+16 p+24 c1 v c2 i p+0 p+8 p+16 p+20 p+24 struct S3 { Draw the allocation for this structure float x[2]; Largest data first What is K? int i[2]; char c; p must be multiple of 4 struct S5 { } *p; double v; 2 bytes wasted char c1; x[0] x[1] i[0] i[1] c char c2; v c1 c2 i int i; p+0 p+4 p+8 p+12 p+16 p+20 p+0 p+8 p+12 p+16 } *p; – 17 – – 18 – Practice problem 3.44 Practice problem 3.45 For each of the following structure declarations, What are the byte offsets of each field? determine the offset of each field, the total size of the struct { 0 8 16 24 28 32 40 48 structure, and its alignment requirement char *a; short b; struct P1 {int i; char c; int j; char d;}; double c; char d; 0, 4, 8, 12 : 16 bytes : 4 What is the total size of the structure? float e; struct P2 {int i; char c; char d; long j;}; char f; long g; 0, 4, 5, 8 : 16 bytes : 8 Must be multiple of K (8) => 56 int h; struct P3 {short w[3]; char c[3];}; } rec; 0, 6 : 10 bytes : 2 Rearrange the structure to minimize space struct P4 {short w[5]; char *c[3];}; 0, 16 : 40 bytes : 8 a, c, g, e, h, b, d, f struct P5 {struct P3 a[2]; struct P2 t} Answer the two questions again 0, 24 : 40 bytes : 8 0 8 16 24 28 32 34 35 Multiple of 8 => 40 – 19 – – 20 – 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