> ## 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.

# C++ Sizeof

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:

```cpp theme={"dark"}
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.

```cpp theme={"dark"}
#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.

```cpp theme={"dark"}
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.

```cpp theme={"dark"}
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.

```cpp theme={"dark"}
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.

```cpp theme={"dark"}
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.

```cpp theme={"dark"}
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.

```cpp theme={"dark"}
#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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
