Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Keywords and Statements

The sizeof and alignof operators

Reference for C++ sizeof, sizeof..., alignof and alignas: sizes of types and objects, padding in structs, array sizes, decay in functions and std::size.

sizeof yields the size, in bytes, of a type or of the object an expression would produce, as a compile-time constant of type std::size_t. alignof yields the alignment requirement of a type: the number of bytes between successive addresses at which an object of that type may be placed.

sizeof(char) is always 1 by definition; everything else is implementation-defined. A struct's size can exceed the sum of its members because the compiler inserts padding to satisfy alignment. These operators never evaluate their operand.

C++
static_assert(sizeof(char) == 1);
static_assert(alignof(double) == 8);   // typical on 64-bit platforms

Try it

C++ Sizes, padding and member order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Array decay, sizeof..., alignas and std::size

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
sizeof(type)            // size of a type
sizeof expression       // size of the type of an expression (parentheses optional)
sizeof...(pack)         // number of elements in a parameter pack (C++11)
alignof(type)           // alignment requirement (C++11)
alignas(N) / alignas(type)   // request stricter alignment for a variable or type

Result: a std::size_t constant. Errors: sizeof of an incomplete type, a function, or a bit-field is ill-formed.

Usage notes

  • sizeof on a reference type gives the size of the referred object. On a class it includes padding and any hidden members such as a vtable pointer.
  • Size of an array object is the whole array (sizeof(arr) is 40 for ten 4-byte ints), but an array parameter in a function has decayed to a pointer, so sizeof returns the pointer size. Use std::size(arr), a std::array, or a std::span instead of the old sizeof(a)/sizeof(a[0]).
  • Reorder members from largest alignment to smallest to reduce padding. Layout is implementation-defined, and the numbers you see (8-byte double alignment on x86-64) are typical, not guaranteed.
  • An empty class has sizeof at least 1 so distinct objects have distinct addresses. [[no_unique_address]] (C++20) lets an empty member take no space.
  • alignof(std::max_align_t) is the alignment new guarantees without alignas. Over-aligned types work with new since C++17.
  • Use sizeof with static_assert to lock in layout assumptions about structures sent over networks or stored in files.
  • sizeof of an expression never evaluates it: sizeof(i++) leaves i unchanged and sizeof(*ptr) is valid for a null ptr.
  • Bit counts: sizeof(T) * CHAR_BIT (from <climits>), or std::numeric_limits<T>::digits for value bits.