Dynamic Memory Allocation I

advertisement
Data Representation and Alignment
Topics

Simple static allocation and alignment of
basic types and data structures
 Policies
 Mechanisms
Data Representations
Sizes of C Objects (in Bytes)

C Data Type
Intel IA32
 int
 long int
 char
 short
 float
 double
 char *
» Or any other pointer

–2–
A word is 4 bytes
4
4
1
2
4
8
4
(1 word)
(2 words)
Alignment
Aligned Data



Primitive data type requires K bytes
Address must be multiple of K
Required on some machines; advised on Intel
Motivation for Aligning Data

Memory accessed by aligned double words (or quad-words)
 Inefficient to load or store datum that spans quad word
boundaries
Compiler

–3–
Inserts gaps in structure to ensure correct alignment of
fields
Structures
Concept



Contiguously-allocated region of memory
Refer to members within structure by names
Members may be of different types
struct rec {
int i;
int a[3];
int *p;
};
–4–
Memory Layout
i
0
a
4
p
16 20
Specific Cases of Alignment
Size of Primitive Data Type:

1 byte (e.g., char)
 no restrictions on address

2 bytes (e.g., short)
 lowest 1 bit of address is 0(2)

4 bytes (e.g., int, float, char *, etc.)
 lowest 2 bits of address - 00(2)

8 bytes (e.g., double)
 lowest 3 bits of address 000(2)
–5–
Satisfying Alignment with Structures
Offsets Within Structure

Must satisfy element’s alignment requirement
Overall Structure Placement

Each structure has alignment requirement K
 Largest alignment of any element

–6–
Initial address & structure length must be
multiples of K
Example (under Linux)
 K = 4, due to int element
c
p+0
i[0]
p+4
Multiple of 4
Multiple of 4
–7–
struct S1 {
char c;
int i[2];
} *p;
i[1]
p+8
p+12
Multiple of 4
Overall Alignment Requirement
struct S2{
float x[2];
int i[2];
char c;
} *p;
p must be multiple of 4
x[0]
p+0
–8–
x[1]
p+4
i[0]
p+8
i[1]
p+12
c
p+16
p+20
Arrays of Structures
Principle

Allocated by repeating allocation
for array type
a[1].i
a[1].v
a+12
a+16
a[0]
a+0
–9–
a[1].j
a+20
a[1]
a+12
struct S3 {
short i;
float v;
short j;
} a[10];
a+24
•••
a[2]
a+24
a+36
Satisfying Alignment within Structure
Achieving Alignment

Starting address of structure array must be
multiple of worst-case alignment for any element
 a must be multiple of 4

Offset of element within structure must be
multiple of element’s alignment requirement
 v’s offset of 4 is a multiple of 4

struct S3 {
short i;
float v;
short j;
} a[10];
Overall size of structure must be multiple of
worst-case alignment for any element
 Structure padded with unused space to be 12
bytes
a[0]
• • •
a[i]
a+12i
a+0
a[1].i
Multiple of 4
– 10 –
a+12i
• • •
a[1].v
a+12i+4
Multiple of 4
a[1].j
Array Allocation
Basic Principle
T A[L];


Array of data type T and length L
Contiguously allocated region of L * sizeof(T) bytes
char string[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
char *p[3];
x
x+4
x+8
x + 12
x + 24
Multidimensional (Nested) Arrays
Declaration
T A[R][C];
 2D array of data type T
 R rows, C columns
 T element requires K bytes
A[0][0]
•
•
•
Array Size

• • •
A[0][C-1]
•
•
•
A[R-1][0] • • • A[R-1][C-1]
R * C * K bytes
Arrangement

Row-Major Ordering (C code)
int A[R][C];
A
[0]
[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]
Summary
Arrays in C



Contiguous allocation of memory
Pointer to first element
No bounds checking
Structures


– 13 –
Allocate bytes in order declared
Pad in middle and at end to satisfy alignment
Misc: Boolean (“Bitwise”) Operators
Operate on numbers as Bit Vectors

Operations applied bitwise
01101001
& 01010101
01000001
– 14 –
01101001
| 01010101
01111101
01101001
^ 01010101
00111100
~ 01010101
10101010
Download