0% found this document useful (0 votes)
0 views10 pages

Lecture Slides 06 065-Structsalign

Uploaded by

yihuangece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views10 pages

Lecture Slides 06 065-Structsalign

Uploaded by

yihuangece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

University of Washington

Section 5: Arrays & Other Data Structures


 Array allocation and access in memory
 Multi-dimensional or nested arrays
 Multi-level arrays
 Other structures in memory
 Data structures and alignment

Structures and Alignment


University of Washington

Structures & Alignment


 Unaligned Data struct
struct S1
S1 {{
char
char c;
c;
c i[0] i[1] v int
int i[2];
i[2];
p p+1 p+5 p+9 p+17 double
double v;v;
}} *p;
*p;
 Aligned Data
 Primitive data type requires K bytes
 Address must be multiple of K

c 3 bytes i[0] i[1] 4 bytes v


p+0 p+4 p+8 p+16 p+24

Multiple of 4 Multiple of 8

Multiple of Multiple of
8 Structures and Alignment
8
University of Washington

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 on IA32
 Treated differently by IA32 Linux, x86-64 Linux, and Windows!
 What is the motivation for alignment?

Structures and Alignment


University of Washington

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 on IA32
 Treated differently by IA32 Linux, x86-64 Linux, and Windows!
 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 quad word 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

Structures and Alignment


University of Washington

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 the same as a 4-byte primitive data type
 12 bytes: long double
 Windows, Linux: lowest 2 bits of address must be 002

Structures and Alignment


University of Washington

Satisfying Alignment with Structures


struct S1 {
 Within structure: char c;
 Must satisfy every member’s alignment requirement int i[2];
 Overall structure placement double v;
} *p1;
 Each structure has alignment requirement K
 K = Largest alignment of any element
 Initial address & structure length must be multiples of K
 Example (under Windows or x86-64): K = ?
 K = 8, due to double member

c 3 bytes i[0] i[1] 4 bytes v


p1+0 p1+4 p1+8 p1+16 p1+24

Multiple of 4 Multiple of 8

Multiple of 8 Multiple of 8
Structures and Alignment
University of Washington

Different Alignment Conventions


struct S1 {
 IA32 Windows or x86-64: char c;
int i[2];
 K = 8, due to double member double v;
} *p1;

c 3 bytes i[0] i[1] 4 bytes v


p1+0 p1+4 p1+8 p1+16 p1+24

 IA32 Linux: K = ?
 K = 4; double aligned like a 4-byte data type

c 3 bytes i[0] i[1] v


p1+0 p1+4 p1+8 p1+12 p1+20

Structures and Alignment


University of Washington

Saving Space
 Put large data types first:
struct S1 { struct S2 {
char c; double v;
int i[2]; int i[2];
double v; char c;
} *p1; } *p2;

 Effect (example x86-64, both have K=8)

c 3 bytes i[0] i[1] 4 bytes v


p1+0 p1+4 p1+8 p1+16 p1+24

Unfortunately, doesn’t
satisfy requirement
v i[0] i[1] c that struct’s total size
p2+0 p2+8 p2+16 is a multiple of K
Structures and Alignment
University of Washington

Arrays of Structures
 Satisfy alignment requirement struct S2 {
for every element double v;
int i[2];
char c;
} a[10];

a[0] a[1] a[2] •••


a+0 a+24 a+48 a+72

v i[0] i[1] c 7 bytes

a+24 a+32 a+40 a+48

Structures and Alignment


University of Washington

Accessing Array Elements // Global:


struct S3 {
short i;
 Compute array offset 12i (sizeof(S3)) float v;
short j;
 Element j is at offset 8 within structure } a[10];
 Since a is static array, assembler gives offset a+8

a[0] • • • a[i] • • •
a+0 a+12i

i 2 bytes v j 2 bytes

a+12i a+12i+8
short get_j(int idx)
{ # %eax = idx
return a[idx].j; leal (%eax,%eax,2),%eax # 3*idx
// return (a + idx)->j; movswl a+8(,%eax,4),%eax # a+12*idx+8
}
Structures and Alignment

You might also like