Skip to content
Tutorials Center
My learning

Topics

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.

C++
std::size_t n = sizeof(int);       // type form: parentheses required
std::size_t m = sizeof n;          // expression form: parentheses optional

Try it

C++ Types, expressions and unevaluated operands

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++ Arrays, decay, pack size and padding

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-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 where arr is still an array. A function parameter written int a[10] or int a[] is a pointer, so sizeof(a) is the pointer size. Prefer std::size(arr), std::ssize, std::array or std::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. A std::vector's sizeof does not include its elements.
  • Type of the result: std::size_t is unsigned, so sizeof(a) - sizeof(b) can wrap around. Use std::ssize for signed counts.
  • Precedence: sizeof is a unary operator, so sizeof x * 2 is (sizeof x) * 2. See precedence.
  • Pitfall: sizeof on a char expression gives 1 even when you want a string length; use std::strlen or .size().
  • Portability: don't hard-code sizes (the width of long is 4 bytes on Windows and 8 on 64-bit Linux). Use the fixed-width types from <cstdint>, or static_assert(sizeof(T) == N) to document an assumption.