Article

C++ reminder: Function-local static variables are initialized only once, even if it looks like they get initialized multiple times

The Old New Thing ·The Old New Thing ·Published 2026-09-28 ·5 min read

This article explains the behavior of function-local static variables in C++, emphasizing that they are initialized only once at the first point of declaration. Subsequent accesses retain the variable's value without reinitialization.

From the article

When you write a static variable inside a function, it is initialized only once, specifically at the first time that execution reaches the variable’s declaration , If execution reaches the variable again in the future, no initialization occurs. It just retains its old value.

Some time ago, I noted an attempt to fix a lifetime issue by making a variable static.

The original code used this header from an external widget library:


Share this resource


Discovered 2026-09-29 Source The Old New Thing Archive 2026-09 →