Alignment university of washington structures

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ture by names   Members may be of different types Structures University of Washington Structures   Accessing Structure Member   Given an instance of the struct, we can use struct rec { int i; int a[3]; int *p; }; the . operator, just like Java:   struct rec r1; r1.i = val;   What if we have a pointer to a struct: struct rec *r = &r1; (*r).i = val;   Using * and . operators: = val;   Or, use -> operator for short: r->i   Pointer indicates first byte of structure; access members with offsets void set_i(struct rec *r, int val) { r->i = val; } IA32 Assembly # %eax = val # %edx = r movl %eax,(%edx) Structures # Mem[r] = val University of Washington GeneraMng Pointer to Structure Member struct rec { int i; int a[3]; int *p; };   GeneraMng Pointer to Array Element   Offset of each structure member determined at compile Nme r r+4+4*idx ia 0 p 4 16 20 int *find_a (struct rec *r, int idx) { return &r->a[idx]; } # %ecx = idx # %edx = r leal 0(,%ecx,4),%eax # 4*idx leal 4(%eax,%edx),%eax # r+4*idx+4 Structures University of Washington Arrays & Other Data Structures           Array allocaMon and access in memory MulM ­dimensional or nested arrays MulM ­level arrays Other structures in memory Data structures and alignment Structures and Alignment University of Washington Structures & Alignment Unaligned Data   c i[0] i[1] p p+1   v struct S1 { char c; int i[2]; double v; } *p; Aligned Data p+5 p+9 p+17   PrimiNve data type requires K bytes   Address must be mulNple of K c p+0 3 bytes i[0] p+4 MulMple of 4 i[1] 4 bytes p+8 v p+16 p+24 MulMple of 8 MulMple of 8 MulMple of 8 Structures and Alignment University of Washington Alignment Principles   Aligned Data   PrimiNve data type requires K bytes   Address must be mulNple of K   Aligned data is required on some machines; it is advised on IA32   Treated differently by IA32 Linux, x86 ­64 Linux, and Windows!   What is the moMvaMon for alignment? Structures and Alignment University of Washington Alignment Principles   Aligned Data   PrimiNve data type requires K bytes ...
View Full Document

Ask a homework question - tutors are online