Title: CSE 351 23au / LS14 Structs & Alignment

Q: In C, a user-defined structured group of variables is known as a ___
A: struct

Q: I want to define a new struct called `song` that clumps together the
- songTitle as a char*,
- lengthInSeconds as an int, and
- yearReleased as an int.
Fill in the struct definition:
___ ___ {
    char* ___;
    ___ lengthInSeconds;
    int yearReleased;
} ___
A: struct song { // song is our struct tag
    char* songTitle; // song has 3 struct fields
    int lengthInSeconds;
    int yearReleased;
}; //  don't forget the semicolon at the end of the definition

Q: True or false: Similar to arrays, a struct instance is one contiguous chunk of memory
A: True!

Q: What are the two rules used to determine the size and layout of a struct instance in C?
A: 1. user-defined ordering of the fields
2. alignment requirements

Q: When defining a new struct, the ___ and ___ are all user-chosen.
A: struct tag, struct fields

Q: True or false: A struct can hold an instance of itself as a field
A: False, we wouldn’t know how much space to allocate for it
A pointer of the struct type is fine, however.

Q: Because two-word struct data type names (`struct struct_tag`) are cumbersome, we can create aliases to other data types by combining them with ___
A: typedef
We can now use `uli` to refer to an unsigned long int in our code

Q: A primitive data type of size K bytes in memory is considered aligned if its address is a multiple of ___
A: K

Q: Within structs, padding between fields is known as ___
A: internal fragmentation

Q: What does K_max stand for?
A: The maximum K of all the fields (the largest sizeof(T))

Q: What are the two restrictions on the overall struct size?
A: 1. The address of the struck must be a multiple of K_max
2. The size of the overall struct must also be a multiple of K_max

Q: Padding outside of the fields (i.e., between struct instances), is known as ___
A: external fragmentation

Q: Why is external fragmentation important?
A: It maintains proper alignment for all fields in all elements of the array