Upload
uuzumakii-naaruto
View
231
Download
0
Embed Size (px)
Citation preview
7/28/2019 Struct Union MSR
1/18
Structures and Unions in C
7/28/2019 Struct Union MSR
2/18
Structures
Compound data:
A date is
an int month and
an int day and
an int year
struct ADate {int month;
int day;
int year;
};
struct ADate date;
date.month = 9;
date.day = 1;
date.year = 2005;
7/28/2019 Struct Union MSR
3/18
Structure Representation & Size
sizeof(struct ) =sum ofsizeof(field)
+ alignment paddingProcessor- and compiler-specific
6261 EF BE AD DE
c1 c2 ipadding
struct CharCharInt {
char c1;
char c2;
int i;
} foo;
foo.c1 = a;
foo.c2 = b;
foo.i = 0xDEADBEEF;
x86 uses little-endian representation
7/28/2019 Struct Union MSR
4/18
7/28/2019 Struct Union MSR
5/18
Arrays of Structures
#define NFRIENDS 10#define TRUE 1
#define FALSE 0
typedef int bool;
Date birthdays[NFRIENDS];
bool
check_birthday(Date today)
{
int i;
for (i = 0; i < NFRIENDS; i++) {
if ((today.month == birthdays[i].month) &&
(today.day == birthdays[i].day))return (true);
}
return FALSE;
}
Constant
Arra
y declaration
Array index, thenstructure field
7/28/2019 Struct Union MSR
6/18
Pointers to Structures
Datecreate_date1(int month,
int day,
int year)
{
Date d;
d.month = month;
d.day = day;
d.year = year;
return (d);}
voidcreate_date2(Date *d,
int month,
int day,
int year)
{
d->month = month;
d->day = day;
d->year = year;
}
Copies date
Pass-by-reference
Date today;
today = create_date1(9, 4, 2008);
create_date2(&today, 9, 4, 2008);
7/28/2019 Struct Union MSR
7/18
Pointers to Structures (cont.)
void
create_date2(Date *d,
int month,
int day,
int year)
{
d->month = month;d->day = day;
d->year = year;
}
void
foo(void)
{
Date today;
create_date2(&today, 9, 4, 2008);
}
today.month:
today.day:
today.year:
0x1000
0x1004
0x1008
month: 9
day: 4
year: 2008
0x30A0
0x30A4
0x30A8
d: 0x10000x3098
9
4
2008
7/28/2019 Struct Union MSR
8/18
Pointers to Structures (cont.)
Date *create_date3(int month,
int day,
int year)
{
Date *d;
d->month = month;
d->day = day;
d->year = year;
return (d);}
What is d pointing to?!?!
7/28/2019 Struct Union MSR
9/18
Collections of Bools (Bit Vectors)
Byte, word, ... can represent many booleansOne per bit, e.g., 00100101 = false, false, true, ..., true
Bit-wise operations:Bit-wise AND: 00100101 & 10111100 == 00100100
Bit-wise OR: 00100101 | 10111100 == 10111101
Bit-wise NOT: ~ 00100101 == 11011010
Bit-wise XOR: 00100101 ^ 10111100 == 10011001
7/28/2019 Struct Union MSR
10/18
Operations on Bit Vectors
const unsigned int low_three_bits_mask = 0x7;
unsigned int bit_vec = 0x15;
000 0111
001 0101
Always use Cs unsignedtypes for bit vectorsAmaskindicates which bit positions we are interested in
000 0101 == 001 0101 & 000 0111
important_bits = bit_vec & low_three_bits_mask;
Selecting bits:
Result =?
7/28/2019 Struct Union MSR
11/18
Operations on Bit Vectors
const unsigned int low_three_bits_mask = 0x7;
unsigned int bit_vec = 0x15;
000 0111
001 0101
bit_vec |= low_three_bits_mask;
Setting bits:
Result =?
001 0111 == 001 0101 | 000 0111
7/28/2019 Struct Union MSR
12/18
Operations on Bit Vectors
const unsigned int low_three_bits_mask = 0x7;
unsigned int bit_vec = 0x15;
000 0111
001 0101
bit_vec &= ~low_three_bits_mask;
Clearing bits:
Result =?
001 0000 == 001 0101 & ~000 0111
7/28/2019 Struct Union MSR
13/18
Bit-field Structures
Special syntax packsstructure values moretightly
Similar to bit vectors, but
arguably easier to read Nonetheless, bit vectors
are more commonlyused.
Padded to be an integralnumber of words
Placement is compiler-specific.
1 1 0 1 1 0 f1 f2 f3
struct Flags {
int f1:3;
unsigned int f2:1;
unsigned int f3:2;
} foo;
foo.f1 = -2;
foo.f2 = 1;
foo.f3 = 2;
7/28/2019 Struct Union MSR
14/18
Unions
A union is a special data typeavailable in C that enables you tostore different data types in the same
memory location.You can define a union with manymembers, but only one member can
contain a value at any given time.Unions provide an efficient way ofusing the same memory location formulti-purpose.
7/28/2019 Struct Union MSR
15/18
Unions
Choices:
An element is
an int i or
a char c
sizeof(union ) =
maximum ofsizeof(field)
EF BE AD DEc
i
padding
union AnElt {
int i;
char c;
} elt1, elt2;
elt1.i = 4;
elt2.c = a;
elt2.i = 0xDEADBEEF;
7/28/2019 Struct Union MSR
16/18
Unions
A union value doesnt know which case itcontains
union AnElt {
int i;
char c;} elt1, elt2;
elt1.i = 4;
elt2.c = a;
elt2.i = 0xDEADBEEF;
if (elt1 currently has a char)
How should your program keep trackwhether elt1, elt2 hold an int or
a char?
?
?Basic answer: Another variable holds
that info
7/28/2019 Struct Union MSR
17/18
Tagged Unions
Tag every value with its case
I.e., pair the type info together with the unionImplicit in Java, Scheme, ML,
Enum must be external to struct,
so constants are globally visible.
Struct field must be named.
enum Union_Tag {IS_INT, IS_CHAR};
struct TaggedUnion {
enum Union_Tag tag;
union {
int i;
char c;
} data;
};
7/28/2019 Struct Union MSR
18/18
Next Time
Memory Allocation