메인 내용으로 이동

Structs and Unions


  • In Computer Systems, structs are collections of heterogeneous data
    • laid out in consecutive memory locations (used paddings to ensure alignment)
    • reordering can reduce size!
struct node {
short w;
char *p;

... turns into ...

0   2 paddings! 8


  • Elements will be padded to match the size.
  • If the struct has int, 4 bytes in size, it needs to start at a multiple of 4.


  • Union allows elements to read/write in the same memory.
  • All elements start at offset 0.
  • The size of the union is the biggest member.
  • Elements must be plain old data or at least default constructible.
  • Bytes can be stored in reversed order depending on the endianness.