In C++
I know static
and global
objects are constructed before the main
function. But as you know, in C
, there is no such kind initialization procedure
before main
.
For example, in my code:
int global_int1 = 5; int global_int2; static int static_int1 = 4; static int static_int2;
5
and 4
are stored during compilation? How to manage them when initialization?EDIT:
Clarification of 2nd question.
5
to initialize global_int1
, so how can the compiler assign 5
to global_int
? For example, maybe the compiler first store the 5
value at somewhere (i.e. a table), and get this value when initialization begins.Static variables are initialized only once , at the start of the execution. These variables will be initialized first, before the initialization of any instance variables. A single copy to be shared by all instances of the class. A static variable can be accessed directly by the class name and doesn't need any object.
Yes, all members of a are guaranteed to be initialised to 0. If an object that has static storage duration is not initialized explicitly, it is initialized implicitly as if every member that has arithmetic type were assigned 0 and every member that has pointer type were assigned a null pointer constant.
Static variables are initialized only once. Compiler persist the variable till the end of the program. Static variable can be defined inside or outside the function. They are local to the block.
Global (namespace) variables or static class members 1 live for the entire execution of the program: they must be initialized before main() is run and destroyed after execution finishes.
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":
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With