By static and global objects, I presume you mean objects with static lifetime defined at namespace scope. When such objects are defined with local scope, the rules are slightly different.

Formally, C++ initializes such variables in three phases: 1. Zero initialization 2. Static initialization 3. Dynamic initialization The language also distinguishes between variables which require dynamic initialization, and those which require static initialization: all static objects (objects with static lifetime) are first zero initialized, then objects with static initialization are initialized, and then dynamic initialization occurs.

As a simple first approximation, dynamic initialization means that some code must be executed; typically, static initialization doesn't. Thus:

extern int f();

int g1 = 42;    //  static initialization
int g2 = f();   //  dynamic initialization

Another approximization would be that static initialization is what C supports (for variables with static lifetime), dynamic everything else.

How the compiler does this depends, of course, on the initialization, but on disk based systems, where the executable is loaded into memory from disk, the values for static initialization are part of the image on disk, and loaded directly by the system from the disk. On a classical Unix system, global variables would be divided into three "segments":

text: The code, loaded into a write protected area. Static variables with `const` types would also be placed here. data: Static variables with static initializers. bss: Static variables with no-initializer (C and C++) or with dynamic initialization (C++). The executable contains no image for this segment, and the system simply sets it all to `0` before starting your code.

I suspect that a lot of modern systems still use something similar.

EDIT:

One additional remark: the above refers to C++03. For existing programs, C++11 probably doesn't change anything, but it does add constexpr (which means that some user defined functions can still be static initialization) and thread local variables, which opens up a whole new can of worms.

Answer from James Kanze on Stack Overflow
Top answer
1 of 4
30

By static and global objects, I presume you mean objects with static lifetime defined at namespace scope. When such objects are defined with local scope, the rules are slightly different.

Formally, C++ initializes such variables in three phases: 1. Zero initialization 2. Static initialization 3. Dynamic initialization The language also distinguishes between variables which require dynamic initialization, and those which require static initialization: all static objects (objects with static lifetime) are first zero initialized, then objects with static initialization are initialized, and then dynamic initialization occurs.

As a simple first approximation, dynamic initialization means that some code must be executed; typically, static initialization doesn't. Thus:

extern int f();

int g1 = 42;    //  static initialization
int g2 = f();   //  dynamic initialization

Another approximization would be that static initialization is what C supports (for variables with static lifetime), dynamic everything else.

How the compiler does this depends, of course, on the initialization, but on disk based systems, where the executable is loaded into memory from disk, the values for static initialization are part of the image on disk, and loaded directly by the system from the disk. On a classical Unix system, global variables would be divided into three "segments":

text: The code, loaded into a write protected area. Static variables with `const` types would also be placed here. data: Static variables with static initializers. bss: Static variables with no-initializer (C and C++) or with dynamic initialization (C++). The executable contains no image for this segment, and the system simply sets it all to `0` before starting your code.

I suspect that a lot of modern systems still use something similar.

EDIT:

One additional remark: the above refers to C++03. For existing programs, C++11 probably doesn't change anything, but it does add constexpr (which means that some user defined functions can still be static initialization) and thread local variables, which opens up a whole new can of worms.

2 of 4
19

Preface: The word "static" has a vast number of different meanings in C++. Don't get confused.

All your objects have static storage duration. That is because they are neither automatic nor dynamic. (Nor thread-local, though thread-local is a bit like static.)

In C++, Static objects are initialized in two phases: static initialization, and dynamic initialization.

  • Dynamic initialization requires actual code to execute, so this happens for objects that start with a constructor call, or where the initializer is an expression that can only be evaluated at runtime.

  • Static initialization is when the initializer is known statically and no constructor needs to run. (Static initialization is either zero-initialization or constant-initialization.) This is the case for your int variables with constant initializer, and you are guaranteed that those are indeed initialized in the static phase.

  • (Static-storage variables with dynamic initialization are also zero-initialzed statically before anything else happens.)

The crucial point is that the static initialization phase doens't "run" at all. The data is there right from the start. That means that there is no "ordering" or any other such dynamic property that concerns static initialization. The initial values are hard-coded into your program binary, if you will.

Discussions

c - Why are global variables always initialized to '0', but not local variables? - Stack Overflow
Possible Duplicate: Why are global and static variables initialized to their default values? See the code, #include int a; int main(void) { int i; printf("%d %d\n", a, ... More on stackoverflow.com
🌐 stackoverflow.com
c++ - Initializing global variable class - Stack Overflow
C++11 made such a function-local static initialization thread safe as well (quoting the C++0x draft n3242, §6.7:) ... Shute :) I was typing boost::shared_ptr, edited to std::shared_ptr, then thought better of it and changed to scoped_ptr :) Thanks for the heads up! ... +1 for covering number of solutions and aspects. Citation or paragraph number to the referenced spec in C++11 would be nice. ... The constructors of all global objects ... More on stackoverflow.com
🌐 stackoverflow.com
oop - C++ global object initialization fails -- why? and is it possible to put objects in .DATA section? - Stack Overflow
I've used C on microcontrollers for quite some time now. Recently (finally!) I managed to get GCC to compile some c++ code for my µC and started porting some code which was previously only "simula... More on stackoverflow.com
🌐 stackoverflow.com
c++ - Initialize before any global objects - Stack Overflow
I am writing a C++ memory profiler. As everyone knows, an executable program may contain many global objects, and its DLL modules may contain global objects as well. These global objects will be More on stackoverflow.com
🌐 stackoverflow.com
🌐
MIT
web.mit.edu › tibbetts › Public › inside-c › www › initializing-globals.html
The Secret Life of C++: Initializing Globals
Global variables are tricky in C++, since they can have constructors and destructors. Lets look at a simple example of initializing a global object:
🌐
Nrao
casa.nrao.edu › aips2_docs › notes › 173.text
Initialization of Static and Global Variables in C++
In general, for *every* class with ... used to initialize some other static or global object, one must make an "init" class and a static instance of this class, in the include file, to guarantee initialization order....
🌐
Quora
quora.com › Why-do-uninitialized-global-variables-in-C-automatically-initialize-to-a-value-of-0
Why do uninitialized global variables in C automatically initialize to a value of '0'? - Quora
Because of the C language’s memory ... systems: globals that are not explicitly initialized by the program source are placed in a special region that the runtime/loader guarantees starts cleared to zero....
Find elsewhere
🌐
Cppreference
en.cppreference.com › w › cpp › language › initialization.html
Initialization - cppreference.com
December 31, 2024 - Because of the rule above, if initialization of some object o1 refers to a namespace-scope object o2, which potentially requires dynamic initialization, but is defined later in the same translation unit, it is unspecified whether the value of o2 used will be the value of the fully initialized o2 (because the compiler promoted initialization of o2 to compile time) or will be the value of o2 merely zero-initialized.
🌐
TutorialsPoint
tutorialspoint.com › why-are-global-and-static-variables-initialized-to-their-default-values-in-c-cplusplus
Why are global and static variables initialized to their default values in C/C++?
June 26, 2020 - Global and static variables are initialized to their default values because it is in the C or C++ standards and it is free to assign a value by zero at compile time. Both static and global variable behave same to the generated object code. These v
🌐
Learn C++
learncpp.com › cpp-tutorial › introduction-to-global-variables
7.4 — Introduction to global variables – Learn C++
June 19, 2007 - Unlike local variables, which are uninitialized by default, variables with static duration are zero-initialized by default. Non-constant global variables can be optionally initialized:
🌐
Stack Overflow
stackoverflow.com › questions › 30559064 › initialize-before-any-global-objects
c++ - Initialize before any global objects - Stack Overflow
These global objects will be initialized with CRT initialization - after the entry point and before WinMain; for DLLs, the entry point is _DllMainCRTStartup, before you get into DllMain, all the global objects of the DLL are initialized.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › initialization-global-static-variables-c
Initialization of global and static variables in C - GeeksforGeeks
May 8, 2017 - error: initializer element is not constant In C, static and global variables are initialized by the compiler itself. Therefore, they must be initialized with a constant value. Note that the above programs compile and run fine in C++, and produce ...
🌐
Reddit
reddit.com › r/c_programming › how are static and global variables initialized?
r/C_Programming on Reddit: How are static and global variables initialized?
October 5, 2022 -

Say in a function there is a static local variable. How does the function know to only set the value the first time the function is called? Is the local static variable actually stored and initialized identical other global variables and the compiler just hides the variable from other functions?

void function() {
    static int x = 20;
    x++;
    printf("The static variable is: %d", x);
}

My second question is in which order are global and static variables initialized? Some variables might be initialized based on another variable or as a return value of a function which may depend on other global variables. Are there limitations on how global or static variables can be initialized? Is there a requirement that the initial value of all global and static variables must be able to be determined at compile time? Does the compiler execute any functions that initialize global or static variables at compile time and throw an error if it contains anything that cannot be evaluated at compile time?

int a, b, c, d; // global variables
d = time(NULL) // unknown result at compile 
c = a+5;
b = initializeB(); //function may internally use other global variables or other function calls that are unable to be evaluated at compile time 
a = 10;

void function() {
    static int x = initializeX(); //function may internally use other global variables or other function calls that are unable to be evaluated at compile time 
    x++;
    printf("The static variable is: %d", x);
}
🌐
Stack Overflow
stackoverflow.com › questions › 42287786 › initializing-globals-with-variables-in-c
Initializing globals with variables in C - Stack Overflow
March 2, 2017 - ... You have to use constant expressions that can be fully evaluated at compile time. The value in a is not a constant (even if you add const). You could use int *p = &a; — the address of a is a compile time (link time) constant.
🌐
Cplusplus
cplusplus.com › forum › lounge › 116778
Guaranteed order of global initialization
e: ok i understand how it works now... It guarantees it had it's constructor called as there it defines an object in every source file it's used in: That at least doesn't solve the problem cause the global initializer stream is referenced in side the stream constructor.
🌐
Post.Byes
post.bytes.com › home › forum › topic
Global object initialization - Post.Byes
July 28, 2025 - > But for curiosity, why doesn't ... book "Inside C++ Object Model" that for every global[color=blue] > object, the compiler generates an _sti function, which are then called > in sequence before the first user-defined statement of main....