31 KiB
title | TARGET DECK | FILE TAGS | tags | ||
---|---|---|---|---|---|
Alignment | Obsidian::STEM | c17::alignment x86-64 |
|
Overview
For a large class of modern ISAs, storage for basic C datatypes respect self-alignment. This means char
s can start on any byte address, short
s on any even address, 4-byte int
s and float
s must start on an address divisible by 4, and double
s must start on an address divisible by 8. Likewise pointers are also self-aligned.
%%ANKI Basic What does self-alignment refer to? Back: The placement of C datatypes on an address divisible by the size of the datatype. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
Wasted space introduced solely for alignment purposes is referred to as slop.
%%ANKI Cloze With respect to memory alignment, {slop} is {waste space for padding datatypes to their alignment}. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
Structures
A struct
's stride address refers to the first address following the struct
data that has the same alignment as the struct
. In general the compiler adds various constraints to how a struct
is layed out:
- The
struct
's alignment follows that of its widest scalar member.- This guarantees each member satisfies its own self-alignment requirement.
- The
struct
introduces trailing padding up to its stride address.- This ensures each element in an array satsifies its self-alignment requirement.
The sizeof
operator on a struct
returns the total space used between its leading address and its stride address.
%%ANKI
Basic
What addresses can a char
be stored at?
Back: Any address.
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
What addresses can an int
be stored at?
Back: Any address divisible by 4.
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
What addresses can a short
be stored at?
Back: Any address divisible by 2.
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
What addresses can a double
be stored at (outside of a struct
)?
Back: Any address divisible by 8.
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
What addresses can a double
be stored at (inside of a struct
)?
Back: Depending on compiler, addresses divisible by 4 or 8.
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI Basic What addresses can a pointer be stored at? Back: Any address divisible by 8. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI Basic How does self-alignment make access faster? Back: It enables single instruction fetches and puts. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI Basic What value is slop initialized to? Back: Undefined. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
Why isn't equality for struct
s well-defined?
Back: The value of slop is undefined.
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
Why isn't inequality for struct
s well-defined?
Back: The value of slop is undefined.
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI Basic Assume allocated order matches source order. How are the following variables aligned?
char *p;
char c;
short x;
Back:
char *p; // 8 bytes
char c; // 1 byte
char pad[1]; // 1 byte
short x; // 2 bytes
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI Basic Assume allocated order matches source order. How are the following variables aligned?
long x;
short c;
float f;
Back:
long x; // 8 bytes
short c; // 2 bytes
char pad[2]; // 2 bytes
float f; // 4 bytes
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
Assume allocated order matches source order. What is the value of N
?
char c;
char pad1[M];
char *p;
char pad2[N];
int x;
Back: 0
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
Assume allocated order matches source order. What is the value of M
?
char c;
char pad1[M];
char *p;
char pad2[N];
int x;
Back: Between 0
and 7
inclusive.
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
Consider short A[N]
. What is the internal padding of this array?
Back: 0
, i.e. A
has no internal padding.
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
Consider char* A[N]
. What is the internal padding of this array?
Back: 0
, i.e. A
has no internal padding.
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
What is the alignment of a struct
instance?
Back: That of its widest scalar member.
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
Why is the alignment of a struct
instance that of its widest scalar member?
Back: It is an easy way to ensure all members are self-aligned.
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
Why does a struct
sometimes introduce trailing padding?
Back: To ensure each element of an array of these struct
s is self-aligned.
Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.
END%%
%%ANKI
Cloze
The address of a struct
is the same as its {first member}.
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
How much leading padding does a struct
instance have?
Back: 0
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
How are members of the following struct
aligned?
struct foo {
char *p;
char c;
long x;
};
Back:
struct foo {
char *p; // 8 bytes
char c; // 1 byte
char pad[7]; // 7 bytes
long x; // 8 bytes
};
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
How are members of the following struct
aligned?
struct foo {
char c;
char *p;
long x;
};
Back:
struct foo {
char c; // 1 byte
char pad[7]; // 7 bytes
char *p; // 8 bytes
long x; // 8 bytes
};
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Cloze
A struct
's {stride address} is {the first address following the struct
's data with the same alignment as the struct
}.
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
What is the result of sizeof(struct foo)
?
struct foo {
char *p;
char c;
};
Back: 16
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
What is the result of sizeof(struct foo)
?
struct foo {
short s;
char c;
};
Back: 4
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
What "hidden" space is returned by sizeof
on struct
types?
Back: Slop.
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
How are members of the following struct
aligned?
struct foo {
char c;
struct bar {
char *p;
short x;
} bar;
};
Back:
struct foo {
char c; // 1 byte
char pad[7]; // 7 bytes
struct bar {
char *p; // 8 bytes
short x; // 2 bytes
char pad2[6]; // 6 bytes
} bar;
};
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
How are members of the following struct
aligned?
struct foo {
char c;
struct bar {
short x;
} bar;
};
Back:
struct foo {
char c; // 1 byte
char pad[1]; // 1 byte
struct bar {
short x; // 2 bytes
} bar;
};
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
What is the alignment of a nested struct
?
Back: That of its widest scalar member.
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
The sizeof
operator on struct
s returns the space used between what two addresses?
Back: The struct
's leading address and stride address.
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.
END%%
%%ANKI
Basic
What is the offset of field u
in the following?
struct foo {
char u;
int v;
};
Back: 0
Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.
END%%
%%ANKI
Basic
What is the offset of field v
in the following?
struct foo {
char u;
int v;
};
Back: 4
Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.
END%%
Unions
The sizeof
operator on a union
returns that of its widest member.
%%ANKI
Basic
The sizeof
operator on union
s returns what?
Back: The sizeof
that of its widest member.
Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.
END%%
%%ANKI
Basic
What is the offset of field u
in the following?
union foo {
char u;
int v;
};
Back: 0
Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.
END%%
%%ANKI
Basic
What is the offset of field v
in the following?
union foo {
char u;
int v;
};
Back: 0
Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.
END%%
Bit-Fields
A member of a structure or union may be declared to consist of a specified number of bits (including a sign bit, if any). Such a member is called a bit-field; its width is preceded by a colon. Its width must be an ICE with a nonnegative value that does not exceed the width of an object of the type that would be specified were the colon and expression omitted.
A bit-field shall have a type is a qualified or unqualified version of _Bool
, signed int
, unsigned int
, or some other implementation-defined type.
%%ANKI
Basic
A bit-field can be declared as members of what?
Back: struct
s or union
s.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
How is a bit-field declared?
Back: As a member of a struct
or union
with width preceded by a colon.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic What kind of expression must the width of a bit-field be? Back: An ICE. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
What non-implementation-defined types can a bit-field be declared with?
Back: _Bool
, signed int
, or unsigned int
.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
What types can a bit-field be declared with?
Back: _Bool
, signed int
, unsigned int
, or some other implementation-defined type.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic What is the sign of the following bit-field?
struct foo { int bar : 1; };
Back: N/A. This is implementation-defined. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic What is the sign of the following bit-field?
struct foo { signed bar : 1; };
Back: Signed. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic What is the sign of the following bit-field?
struct foo { unsigned bar : 1; };
Back: Unsigned. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Which unary operator cannot be applied to a bit-field object?
Back: &
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic What compilation error exists in the following translation unit?
#include <stdio.h>
struct foo { int a; int b : 4; };
int main(void) {
struct foo bar = { .a = 1, .b = 1 };
printf("%p\n", (void *)&bar.b);
}
Back: Cannot take the address-of a bit-field. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic What compilation error exists in the following translation unit?
#include <stdio.h>
struct foo { int a; int b : 4; };
int main(void) {
struct foo bar = { .a = 1, .b = 1 };
printf("%p\n", (void *)&bar.a);
}
Back: N/A. This compiles correctly. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic What is an unnamed bit-field? Back: A bit-field with no declarator. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
Packing
An implementation may allocate any addressable storage unit large enough to hold a bit-field. If enough space remains, a bit-field that immediately follows another in a structure shall be packed into adjacent bits of the same unit.
If insufficient space remains, whether a bit-field that does not fit is put into the next unit or overlaps adjacent units is implementation-defined.
A bit-field structure member with a width of 0
indicates that no further bit-field is to be packed into the unit in which the previous bit-field, if any, was placed.
%%ANKI Basic What are the addressable storage units supported in x86-64? Back: A byte, word, double word, or quad word. Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016. Tags: x86-64
END%%
%%ANKI Basic What does the C standard mean by an "addressable storage unit"? Back: A memory unit that can be directly addressed/manipulated by the processor. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic Does x86-64 allow bit-fields to overlap adjacent addressable storage units? Back: No. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/. Tags: x86-64
END%%
%%ANKI
Basic
What does a bit-field of width 0
indicate?
Back: No subsequent bit-field can be packed into the unit in which the previous bit-field, if any, was placed.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
What is wrong with the following struct
definition?
struct foo { unsigned bar : 0; };
Back: A bit-field of width 0
cannot have a declarator.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
How is the following struct
correctly written?
struct foo { unsigned bar : 0; };
Back:
struct foo { unsigned : 0; };
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Assume a 4
-byte unsigned int
. What is wrong with the following struct
definition?
struct foo { unsigned bar : 31; };
Back: N/A. This is correct. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf. Tags: x86-64
END%%
%%ANKI
Basic
Assume a 4
-byte unsigned int
. What is wrong with the following struct
definition?
struct foo { unsigned bar : 32; };
Back: N/A. This is correct. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf. Tags: x86-64
END%%
%%ANKI
Basic
Assume a 4
-byte unsigned int
. What is wrong with the following struct
definition?
struct foo { unsigned bar : 33; };
Back: The width of a bit-field cannot exceed its types (in this case unsigned int
).
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
Tags: x86-64
END%%
%%ANKI Basic Assume a byte-sized storage unit, no overlapping units, and low-to-high order. How is the following packed in memory?
struct foo {
signed a : 4;
signed b : 2;
signed c : 4;
};
Back:
struct foo {
_padding : 2; // 2 bits
signed b : 2; // 2 bits
signed a : 4; // 4 bits
_padding : 4; // 4 bits
signed c : 4; // 4 bits
};
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic Assume a byte-sized storage unit, overlapping units, and low-to-high order. How is the following packed in memory?
struct foo {
signed a : 4;
signed : 0;
signed c : 4;
};
Back:
struct foo {
_padding : 4; // 4 bits
signed a : 4; // 4 bits
_padding : 4; // 4 bits
signed c : 4; // 4 bits
};
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic Assume a byte-sized storage unit, overlapping units, and high-to-low order. How is the following packed in memory?
struct foo {
signed a : 4;
signed : 0;
signed c : 4;
};
Back:
struct foo {
signed a : 4; // 4 bits
_padding : 4; // 4 bits
signed c : 4; // 4 bits
_padding : 4; // 4 bits
};
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Assume a 32
-bit storage unit, no overlapping units, and low-to-high order. How is the following packed in memory?
struct foo {
signed a : 4;
signed b : 2;
signed c : 4;
};
Back:
struct foo {
_padding : 22; // 22 bits
signed c : 4; // 4 bits
signed b : 2; // 2 bits
signed a : 4; // 4 bits
};
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic Assume a byte-sized storage unit, overlapping units, and low-to-high order. How is the following packed in memory?
struct foo {
signed a : 4;
signed b : 2;
signed c : 4;
};
Back:
struct foo {
_padding : 6; // 6 bits
signed c : 4; // 4 bits
signed b : 2; // 2 bits
signed a : 4; // 4 bits
};
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Assume a 32
-bit storage unit, no overlapping units, and low-to-high order. How is the following packed in memory?
struct foo {
signed a : 4;
signed : 0;
signed c : 4;
};
Back:
struct foo {
_padding : 28; // 28 bits
signed c : 4; // 4 bits
_padding : 28; // 28 bits
signed a : 4; // 4 bits
};
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic Assume a byte-sized storage unit, overlapping units, and high-to-low order. How is the following packed in memory?
struct foo {
signed a : 4;
signed b : 2;
signed c : 4;
};
Back:
struct foo {
signed a : 4; // 4 bits
signed b : 2; // 2 bits
signed c : 4; // 4 bits
_padding : 6; // 6 bits
};
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic Assume a byte-sized storage unit, no overlapping units, and high-to-low order. How is the following packed in memory?
struct foo {
signed a : 4;
signed b : 2;
signed c : 4;
};
Back:
struct foo {
signed a : 4; // 4 bits
signed b : 2; // 2 bits
_padding : 2; // 2 bits
signed c : 4; // 4 bits
_padding : 4; // 4 bits
};
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Assume a 32
-bit storage unit, overlapping units, and high-to-low order. How is the following packed in memory?
struct foo {
signed a : 4;
signed b : 2;
signed c : 4;
};
Back:
struct foo {
signed a : 4; // 4 bits
signed b : 2; // 2 bits
signed c : 4; // 4 bits
_padding : 22; // 22 bits
};
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic What implementation-defined property guarantees tightly-packed bit-fields? Back: Whether bit-fields can overlap adjacent addressable storage units. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Cloze An implementation may allocate any {addressable storage} unit large enough to hold a bit-field. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic What does it mean for bit-fields to be allocated low-to-high? Back: They are packed starting from the least significant bit of the addressable storage unit. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic What does it mean for bit-fields to be allocated high-to-low? Back: They are packed starting from the most significant bit of the addressable storage unit. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
Bibliography
- Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.
- “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
- Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.