notebook/notes/c17/alignment.md

15 KiB

title TARGET DECK FILE TAGS tags
Alignment Obsidian::STEM c17::alignment x86-64
c17
x86-64

Overview

For a large class of modern ISAs, storage for basic C datatypes respect self-alignment. This means chars can start on any byte address, shorts on any even address, 4-byte ints and floats must start on an address divisible by 4, and doubles must start on an address divisible by 8. Likewise pointers are also self-aligned.

Wasted space introduced solely for alignment purposes is referred to as slop.

Structures

A struct's stride address refers to the first address following the struct data that has the same alignment as the struct. In general the compiler adds various constraints to how a struct is layed out:

  1. The struct's alignment follows that of its widest scalar member.
    • This guarantees each member satisfies its own self-alignment requirement.
  2. The struct introduces trailing padding up to its stride address.
    1. This ensures each element in an array satsifies its self-alignment requirement.

The sizeof operator on a struct returns the total space used between its leading address and its stride address.

%%ANKI Basic What does self-alignment refer to? Back: The placement of C datatypes on an address divisible by the size of the datatype. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic What addresses can a char be stored at? Back: Any address. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic What addresses can an int be stored at? Back: Any address divisible by 4. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic What addresses can a short be stored at? Back: Any address divisible by 2. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic What addresses can a double be stored at (outside of a struct)? Back: Any address divisible by 8. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic What addresses can a double be stored at (inside of a struct)? Back: Depending on compiler, addresses divisible by 4 or 8. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic What addresses can a pointer be stored at? Back: Any address divisible by 8. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic How does self-alignment make access faster? Back: It enables single instruction fetches and puts. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Cloze With respect to memory alignment, {slop} is {waste space for padding datatypes to their alignment}. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic What value is slop initialized to? Back: Undefined. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic Why isn't equality for structs well-defined? Back: The value of slop is undefined. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic Why isn't inequality for structs well-defined? Back: The value of slop is undefined. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic Assume allocated order matches source order. How are the following variables aligned?

char *p;
char c;
short x;

Back:

char *p;     // 8 bytes
char c;      // 1 byte
char pad[1]; // 1 byte
short x;     // 2 bytes

Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic Assume allocated order matches source order. How are the following variables aligned?

long x;
short c;
float f;

Back:

long x;      // 8 bytes
short c;     // 2 bytes
char pad[2]; // 2 bytes
float f;     // 4 bytes

Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic Assume allocated order matches source order. What is the value of N?

char c;
char pad1[M];
char *p;
char pad2[N];
int x;

Back: 0 Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic Assume allocated order matches source order. What is the value of M?

char c;
char pad1[M];
char *p;
char pad2[N];
int x;

Back: Between 0 and 7 inclusive. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic Consider short A[N]. What is the internal padding of this array? Back: 0, i.e. A has no internal padding. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic Consider char* A[N]. What is the internal padding of this array? Back: 0, i.e. A has no internal padding. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic What is the alignment of a struct instance? Back: That of its widest scalar member. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic Why is the alignment of a struct instance that of its widest scalar member? Back: It is an easy way to ensure all members are self-aligned. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic Why does a struct sometimes introduce trailing padding? Back: To ensure each element of an array of these structs is self-aligned. Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.

END%%

%%ANKI Cloze The address of a struct is the same as its {first member}. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic How much leading padding does a struct instance have? Back: 0 Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic How are members of the following struct aligned?

struct foo {
  char *p;
  char c;
  long x;
};

Back:

struct foo {
  char *p;     // 8 bytes
  char c;      // 1 byte
  char pad[7]; // 7 bytes
  long x;      // 8 bytes
};

Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic How are members of the following struct aligned?

struct foo {
  char c;
  char *p;
  long x;
};

Back:

struct foo {
  char c;      // 1 byte
  char pad[7]; // 7 bytes
  char *p;     // 8 bytes
  long x;      // 8 bytes
};

Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Cloze A struct's {stride address} is {the first address following the struct's data with the same alignment as the struct}. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic What is the result of sizeof(struct foo)?

struct foo {
  char *p;
  char c;
};

Back: 16 Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic What is the result of sizeof(struct foo)?

struct foo {
  short s;
  char c;
};

Back: 4 Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic What "hidden" space is returned by sizeof on struct types? Back: Slop. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic How are members of the following struct aligned?

struct foo {
  char c;
  struct bar {
    char *p;
    short x;
  } bar;
};

Back:

struct foo {
  char c;         // 1 byte
  char pad[7];    // 7 bytes
  struct bar {
    char *p;      // 8 bytes
    short x;      // 2 bytes
    char pad2[6]; // 6 bytes
  } bar;
};

Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic How are members of the following struct aligned?

struct foo {
  char c;
  struct bar {
    short x;
  } bar;
};

Back:

struct foo {
  char c;         // 1 byte
  char pad[1];    // 1 byte
  struct bar {
    short x;      // 2 bytes
  } bar;
};

Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic What is the alignment of a nested struct? Back: That of its widest scalar member. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic The sizeof operator on structs returns the space used between what two addresses? Back: The struct's leading address and stride address. Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.

END%%

%%ANKI Basic What is the offset of field u in the following?

struct foo {
  char u;
  int v;
};

Back: 0 Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.

END%%

%%ANKI Basic What is the offset of field v in the following?

struct foo {
  char u;
  int v;
};

Back: 4 Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.

END%%

Unions

The sizeof operator on a union returns that of its widest member.

%%ANKI Basic The sizeof operator on unions returns what? Back: The sizeof that of its widest member. Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.

END%%

%%ANKI Basic What is the offset of field u in the following?

union foo {
  char u;
  int v;
};

Back: 0 Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.

END%%

%%ANKI Basic What is the offset of field v in the following?

union foo {
  char u;
  int v;
};

Back: 0 Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.

END%%

Bibliography

  • Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.
  • Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.