Stories
Slash Boxes
Comments

SoylentNews is people

SoylentNews is powered by your submissions, so send in your scoop. Only 7 submissions in the queue.

Submission Preview

Link to Story

Esoteric: Learn about Structure Packing and Padding

Accepted submission by ElizabethGreene mailto:elizabeth.a.greene@gmail.com at 2025-01-02 16:59:31 from the not-covered-in-CS-class dept.
Software

Eric Raymond has a lovely essay here: http://www.catb.org/esr/structure-packing/ [catb.org] that describes some non-intuitive behavior in how compilers assemble structures in memory. The default is to pad structures out with empty bytes to align data types around arbitrary byte boundaries for similarly non-intuitive reasons.

If you don't immediately understand why this struct is 12 or 16 bytes long, it's worth reading.
struct Foo {
  char *p; /* 4 or 8 bytes */
  char c; /* 1 byte */
  int x; /* 4 bytes */
}


Original Submission