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.
static_assert(sizeof(char) == 1); static_assert(alignof(double) == 8); // typical on 64-bit platforms
Try it
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.
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
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
sizeofon 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, sosizeofreturns the pointer size. Usestd::size(arr), astd::array, or astd::spaninstead of the oldsizeof(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
doublealignment on x86-64) are typical, not guaranteed. - An empty class has
sizeofat 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 alignmentnewguarantees withoutalignas. Over-aligned types work withnewsince C++17.- Use
sizeofwithstatic_assertto lock in layout assumptions about structures sent over networks or stored in files. sizeofof an expression never evaluates it:sizeof(i++)leavesiunchanged andsizeof(*ptr)is valid for a nullptr.- Bit counts:
sizeof(T) * CHAR_BIT(from<climits>), orstd::numeric_limits<T>::digitsfor value bits.
Related
- Fundamental types
- alignas
- static_assert
- Tutorial: Memory layout, Data types