What value does a C++ local int hold after int x;, compared with int x{}; and a global int x;?
answer
- where the object lives matters
- no initializer means default-initialized
- empty braces mean value-initialized
- static storage is zeroed first
- user-provided constructor skips the zeroing
basics
~20 sA local int x; is default-initialized, which for int means not initialized: its value is indeterminate and reading it is undefined behaviour before C++26. int x{}; value-initializes it to 0, and a global int is zero-initialized because it has static storage duration.
solid answer
~40 sC++ has three related forms. **Default-initialization** happens when there is no initializer: for a class it runs the default constructor, for `int` it does nothing, so a local `int x;` holds an indeterminate value and reading it is undefined behaviour (C++26 reclassifies the read of such a local as *erroneous behaviour*). **Value-initialization** happens with empty parentheses or braces — `int x{}`, `new int()`, `std::make_unique<int>()` — and gives scalars 0. **Zero-initialization** happens to every object with static or thread storage duration before anything else, which is why a global `int x;` is 0. For class types the difference shows up in members: `T{}` zeroes members first only if the default constructor is not user-provided, so a hand-written empty constructor leaves members it does not set indeterminate.
code
cpp · 16 lines#include <iostream>
#include <memory>
struct Defaulted { Defaulted() = default; int frames; }; // not user-provided
struct Provided { Provided() {} int frames; }; // user-provided
int global_count; // static storage: zero-initialized
int main() {
int a{}; // value-initialized: 0
auto p = std::make_unique<int>(); // new int(): value-initialized: 0
Defaulted d{}; // zero-initialized first: frames == 0
Provided q{}; // only the constructor runs: frames indeterminate
std::cout << a << ' ' << *p << ' ' << d.frames << ' ' << global_count << '\n';
(void)q; // reading q.frames would be undefined behaviour
}go deeper
Know that a local int without an initializer holds garbage, while int x{} and a global int start at zero.
Explain default, value and zero initialization as three rules, including new int versus new int() and static storage.
Spot the user-provided constructor case where T{} does not zero members, and fix it with default member initializers rather than comments.
Set a rule that every member carries a default member initializer, and decide where make_unique_for_overwrite is allowed for performance.
## Three words for "what is in it at the start" The standard's initialization rules use three terms that sound alike and behave differently: - **Default-initialization** is what you get with no initializer at all. For a class type, the default constructor runs. For an array, each element is default-initialized. For anything else — `int`, `double`, pointers — **no initialization is performed**. - **Value-initialization** is what you get with an empty initializer: `T()`, `T{}`, `new T()`, `new T{}`. For a class type, the object is first zero-initialized *if its default constructor is not user-provided*, and then default-initialized. For a scalar, it is zero-initialized. - **Zero-initialization** sets scalars to the value of `0` converted to the type (0, 0.0, null pointer) and recurses into class members, bases and array elements. On top of that, every object with **static or thread storage duration** — globals, namespace-scope variables, `static` locals, `thread_local` variables — is zero-initialized before any other initialization happens. ## The same int, four ways | Declaration | Form | Value | |---|---|---| | `int x;` at block scope | default-initialized | indeterminate; reading it is undefined behaviour (erroneous behaviour in C++26) | | `int x{};` or `int x = int();` | value-initialized | 0 | | `int x;` at namespace scope | zero-initialized (static storage) | 0 | | `static int x;` inside a function | zero-initialized (static storage) | 0 | Heap objects follow the same split: `new int` is default-initialized and indeterminate, while `new int()` and `new int{}` are value-initialized to 0. `std::make_unique<int>()` uses `new int()` and therefore yields 0; C++20 added `std::make_unique_for_overwrite<int>()` precisely to get default-initialization when you are about to overwrite the value anyway. C++26 changed what reading the local in the first row means: an automatic variable without an initializer now holds an **erroneous value**, so the read is erroneous behaviour (well-defined behaviour that implementations are recommended to diagnose) rather than undefined behaviour. Heap objects from `new int` keep indeterminate values. The lesson on undefined-behaviour sources covers what that buys you; for interview purposes, "uninitialized local reads are a bug" is true in every standard. ## Class types: the user-provided constructor trap Consider two versions of a frame counter: ```cpp struct Defaulted { Defaulted() = default; int frames; }; struct Provided { Provided() {} int frames; }; ``` - `Defaulted d{};` value-initializes. The default constructor is defaulted on its first declaration, so it is **not user-provided**, the object is zero-initialized first, and `d.frames` is 0. - `Provided p{};` also value-initializes, but the constructor is user-provided, so there is no zeroing step. The empty constructor leaves `p.frames` indeterminate. - `Defaulted d;` (no braces) default-initializes and leaves `frames` indeterminate in both cases. The fix is not to memorize this table but to give members **default member initializers** (`int frames = 0;`), so every form of construction starts from a known value. ## Aggregates and arrays For an aggregate such as a window configuration struct, `WindowConfig cfg{};` is aggregate initialization: each member with a default member initializer uses it, and each other member is copy-initialized from an empty initializer list, which zeroes scalars. Arrays behave the same way: `int samples[4]{};` is all zeros, and `int samples[4] = {7};` sets the first element to 7 and the remaining three to 0. Only a bare `int samples[4];` at block scope is left indeterminate. ## Members of a class with a constructor When a class has a user-provided constructor, each member that the constructor's member-initializer list does not mention is default-initialized — unless it has a default member initializer. A constructor that sets `width` and `height` but forgets `frame_count` therefore leaves `frame_count` indeterminate in every object, whichever syntax the caller uses. That is why default member initializers are the robust fix: they apply to every constructor that does not override them, including ones added later by someone else. ## Practical habits 1. Initialize at the declaration: `int count = 0;` or `int count{};`. 2. Give every scalar member a default member initializer. 3. Prefer `std::make_unique<T>()` unless profiling shows the zeroing matters, then use `std::make_unique_for_overwrite`. 4. Build with `-Wall` (which enables `-Wuninitialized` in GCC and Clang) and run Clang's MemorySanitizer on tests to catch reads that compile-time warnings miss.
- Why does Defaulted() = default; zero the member under T{} while Provided() {} does not?Value-initialization zero-initializes a class object first only when the selected default constructor is not user-provided. A constructor explicitly defaulted on its first declaration is not user-provided, so `Defaulted{}` zeroes `frames` before running it. `Provided() {}` is user-provided, so value-initialization just calls it, and the member it never sets stays indeterminate.
- Is a static local int inside a function initialized each time the function runs?No. A `static` local has static storage duration, so it is zero-initialized once before any other initialization; an initializer that needs dynamic initialization runs the first time control passes through the declaration. Later calls see the value left by the previous call. Thread-safe initialization of such locals belongs to the material on storage duration.
- a local int without an initializer starts at zero
- int x{}; leaves x uninitialized like int x;
- new int and new int() both give zero
- empty braces always zero every member of a class
- globals need an explicit = 0 to start at zero
Assumes C++23 rules; C++20 added std::make_unique_for_overwrite, and C++26 turns reading an uninitialized automatic variable into erroneous behaviour instead of undefined behaviour.