Video summary

9 Struct alignment

Main summary

Key takeaways

Educational

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 int starts at an address divisible by 4.
  • An 8-byte long or double starts at an address divisible by 8.
  • A 1-byte char can 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:

  1. Find the largest alignment requirement among the members.
  2. Lay out the members in their declared order, adding padding before a member when needed to align it.
  3. 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

  • int and char: The int uses 4 bytes and the char uses 1, but padding brings the struct size to 8 bytes.
  • long and char: With an 8-byte long, the struct occupies 16 bytes in the lecture’s model.
  • Member order matters:
    • An int, followed by a long, then a char, is calculated as 24 bytes because of padding before the long and at the end.
    • Putting the long first, followed by the int and char, reduces the size to 16 bytes.
    • Similar examples with double, int, and char total 16 or 24 bytes, depending on the order.
  • Arrays inside structs: Consider an array’s elements individually for alignment. For example, five 2-byte short elements occupy 10 bytes; padding may then be needed before an 8-byte long. 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.

Pro

Translate summary in another language

Pro

Ask questions to this video

Chat for follow-up questions, clarifications, and source-backed answers.

Coming soon

Share this summary

Original video