C++ Reference ยท Operators
The sizeof operator
Reference for the C++ sizeof operator: what it measures, unevaluated operands, arrays and decay, padding in structs, sizeof..., and std::size alternatives.
sizeof is a compile-time operator that yields the size in bytes of a type or of the type of an expression, as a constant of type std::size_t. The operand is not evaluated, so sizeof(f()) never calls f and sizeof(*p) is safe even for a null pointer.
The size of char, signed char, unsigned char, std::byte and char8_t is 1 by definition. The sizes of the other types are chosen by the implementation, and the values in this page's examples come from the 64-bit GCC runner. This page covers the operator's rules; sizeof and alignof covers alignment and padding in more depth.
std::size_t n = sizeof(int); // type form: parentheses required std::size_t m = sizeof n; // expression form: parentheses optional
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-id) // size of the type sizeof expression // size of the type of the expression sizeof...(pack) // number of elements in a parameter pack (not a byte count)
| Name | Description |
|---|---|
type-id |
Any complete object type; parentheses are required. |
expression |
Not evaluated. Parentheses are optional, but sizeof (a) + b still means (sizeof a) + b. |
pack |
A template parameter pack. sizeof... yields a std::size_t constant. |
Returns: a std::size_t constant. For a reference type it is the size of the referred type.
Errors / undefined behavior: applying sizeof to an incomplete type, void, a function type or a bit-field is ill-formed. There is no runtime behavior to get wrong, but misusing it on a decayed array gives the wrong answer silently.
Usage notes
- Arrays:
sizeof(arr)is the total byte count only wherearris still an array. A function parameter writtenint a[10]orint a[]is a pointer, sosizeof(a)is the pointer size. Preferstd::size(arr),std::ssize,std::arrayorstd::span. - Padding: a struct may be larger than the sum of its members because of alignment padding. Ordering members from largest to smallest alignment often shrinks it.
- Empty classes have size at least 1 so distinct objects get distinct addresses; an empty base subobject may take no space, and
[[no_unique_address]](C++20) lets a member do the same. - Strings:
sizeof("hello")is 6 (it counts the'\0');sizeof(std::string)is the size of the string object, not its text. - Dynamic memory:
sizeof(*p)is the size of the pointed-to type, never the size of an allocated block. Astd::vector'ssizeofdoes not include its elements. - Type of the result:
std::size_tis unsigned, sosizeof(a) - sizeof(b)can wrap around. Usestd::ssizefor signed counts. - Precedence:
sizeofis a unary operator, sosizeof x * 2is(sizeof x) * 2. See precedence. - Pitfall:
sizeofon acharexpression gives 1 even when you want a string length; usestd::strlenor.size(). - Portability: don't hard-code sizes (the width of
longis 4 bytes on Windows and 8 on 64-bit Linux). Use the fixed-width types from<cstdint>, orstatic_assert(sizeof(T) == N)to document an assumption.