Video summary
9 Struct alignment
Main summary
Key takeaways
Main Ideas: C Struct Alignment
The lecture explains how alignment and padding affect the size and layout of a C structure in memory.
In the simplified model used in the examples, each member must begin at an address divisible by its alignment requirement:
- A 4-byte
intstarts at an address divisible by 4. - An 8-byte
longordoublestarts at an address divisible by 8. - A 1-byte
charcan start at any address.
The compiler inserts padding bytes when needed to place a member at a suitably aligned address. It may also add padding at the end of a struct so that the next struct—such as the next element in an array—starts at an aligned address.
As a result, a struct’s size can be greater than the sum of its members’ sizes. Padding bytes are part of the struct’s allocated space and cannot be used for unrelated data.
Estimating a Struct’s Size
Using the lecture’s assumptions:
- Find the largest alignment requirement among the members.
- Lay out the members in their declared order, adding padding before a member when needed to align it.
- Add trailing padding, if needed, so the total struct size is a multiple of the largest alignment requirement.
The lecture presents this as a practical method for solving alignment questions, while noting that exact alignment rules depend on the system and compiler.
Examples Discussed
intandchar: Theintuses 4 bytes and thecharuses 1, but padding brings the struct size to 8 bytes.longandchar: With an 8-bytelong, the struct occupies 16 bytes in the lecture’s model.- Member order matters:
- An
int, followed by along, then achar, is calculated as 24 bytes because of padding before thelongand at the end. - Putting the
longfirst, followed by theintandchar, reduces the size to 16 bytes. - Similar examples with
double,int, andchartotal 16 or 24 bytes, depending on the order.
- An
- Arrays inside structs: Consider an array’s elements individually for alignment. For example, five 2-byte
shortelements occupy 10 bytes; padding may then be needed before an 8-bytelong. The example totals 24 bytes.
The lecturer contrasts aligned layout with a hypothetical unaligned, tightly packed layout, which could use fewer bytes but may result in less efficient access. The lecture says C implementations generally align structs by default, and compiler-specific options can sometimes alter packing.
The discussion of pages and cache lines is offered as an intuition for why memory is considered in aligned units; it is not a complete account of how C struct alignment is specified.
Speakers and Sources
- Speaker: An unnamed instructor or lecturer explaining struct alignment.
- Source/channel: GO Classes for GATE CS, as identified in the video context.
Rate this summary
Your feedback will help improve summaries.
Improve this summary
Reprocess with a stronger model when the summary feels incomplete or inaccurate.
Translate summary in another language
Ask questions to this video
Chat for follow-up questions, clarifications, and source-backed answers.