Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The sizeof operator is a compile-time unary operator that determines the size, in bytes, of a specific data type or an expression’s resulting type. It returns an unsigned integer of type std::size_t (defined in <cstddef>) representing the exact memory footprint required to store an object of that type, including any internal or trailing padding added by the compiler for alignment.

Syntax

The operator accepts three forms of operands:
sizeof(type)
sizeof expression
sizeof...(parameter_pack)
Parentheses are strictly required when the operand is a type identifier. When the operand is an expression, parentheses are optional but commonly used. The sizeof... variant strictly requires parentheses and applies only to variadic templates.

Core Mechanics

Unevaluated Context When an expression is passed to sizeof, it is treated as an unevaluated operand. The compiler resolves the type of the expression purely at compile-time. No runtime execution occurs, meaning any side effects within the expression are ignored.
#include <cstddef>

int x = 5;
std::size_t s = sizeof(x++); 
// 's' receives the size of an int. 'x' remains 5.
Base Unit Guarantee The C++ standard dictates that the size of char, signed char, and unsigned char is exactly 1. Therefore, sizeof measures memory in multiples of sizeof(char). A C++ byte consists of CHAR_BIT bits (defined in <climits>), which is typically, but not strictly required to be, 8 bits. Arrays When applied to a statically sized array, sizeof yields the total number of bytes occupied by the entire array, not the size of a pointer to its first element.
int arr[10];
sizeof(arr); // Yields 40 (assuming 4-byte int: 10 * 4)
References When applied to a reference type, sizeof yields the size of the referenced type. It does not return the size of the underlying pointer implementing the reference.
double d = 3.14;
double& ref = d;
sizeof(ref); // Yields sizeof(double), typically 8
Classes, Structs, and Unions When applied to a class, struct, or union, the result includes the sizes of all member subobjects, base class subobjects, and any padding bytes inserted to satisfy the alignment requirements (alignof) of the architecture.
struct Padded {
    char a;    // 1 byte
               // 3 bytes of padding
    int b;     // 4 bytes
};
sizeof(Padded); // Yields 8, not 5
Empty Classes In C++, an empty class or struct is guaranteed to have a size of at least 1 byte. This rule ensures that distinct objects of the same type have unique memory addresses.
struct Empty {};
sizeof(Empty); // Yields 1 (or more), never 0
Polymorphism Overhead If a class contains virtual functions or utilizes virtual inheritance, the compiler injects hidden overhead into the object layout. This is typically implemented as a virtual table pointer (vptr). The sizeof operator accounts for this overhead, resulting in a larger size than the sum of the explicitly declared member variables.
struct Polymorphic {
    int data;
    virtual ~Polymorphic() = default;
};
sizeof(Polymorphic); // Yields size of 'data' + size of 'vptr' + padding
Variadic Parameter Packs (sizeof...) Modern C++ provides the sizeof... operator specifically for variadic templates. Instead of returning a size in bytes, sizeof... queries the number of elements contained within a template parameter pack.
#include <cstddef>

template<typename... Args>
constexpr std::size_t pack_count() {
    return sizeof...(Args);
}
// pack_count<int, double, char>() yields 3

Restrictions

The sizeof operator cannot be applied to the following:
  • Incomplete types: Types lacking a full definition, such as forward-declared classes or arrays of unknown bound (e.g., extern int arr[];).
  • Function types: Though it is perfectly valid to apply it to pointers to functions.
  • Bit-fields: You cannot query the size of a bit-field member directly.
  • The void type: void is an incomplete type that cannot be completed.
Master C++ with Deep Grasping Methodology!Learn More