Going by the document here http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2011/n3242.pdf
“Variables with static storage duration (3.7.1) or thread storage
duration (3.7.2) shall be zero-initialized (8.5) before any other
initialization takes place”
If I have everything, i.e class declaration and main() in a single file (a must) I should be able to omit the initialization.
But, if I omit, I get "undefined reference" error during build.
#include <iostream>
using namespace std;
class foo
{
public:
static int array[2];
};
int foo::array[2] = {0}; //I should be able to omit this line
int main()
{
cout << "foo::array[0] = " << foo::array[0] << endl;
return 0;
}
PS: No C++11
I think you are misreading the standard. You can simply drop the = {0} part as the compiler will automatically initialize it with zeros.
You can not leave out the entire line because otherwise you just declare the array but you never define it anywhere - that's what is causing the problem for you.
For a static data member of a class, one must compulsorily provide definition in the implementation file because,
static data has a single piece of storage regardless of how many objects are created, that storage must be defined in a single place. The compiler will not allocate storage for you. The linker will report an error if a static data member is declared but not defined.
Related
Regarding the book "Effective C++" from Scot Meyers, and the 4th item: non-local static objects can be uninitialized before the are used (static in this case means "global", with static life). If you replace it with a local-static object, which is created inside a function that returns a reference to it, the object is then for sure initialized before use.
I always have a file with constants. I declare extern const int a; in an .hpp file and define it in the .cpp file. But can then the same thing happen? a can be uninitialized. Or not? Does the same rule apply for built-in types?
Even though you can, it's not such a good idea to return references to "local-static" variables. The variable was (presumably) declared locally to reduce its scope to just the enclosing function, so attempting to increase its scope in this manner is rather hacky. You could make it a global variable and use something like std::call_once to guarantee it's initialized exactly once on first usage. Returning a mutable reference to a local-static object also raises thread-safety concerns because the function may no longer be re-entrant.
POD types with static storage duration are guaranteed to be zero-initialized. You can also initialize them with a constant expression and the language will guarantee they are initialized before any dynamic initialization takes place. Here's a similar question that may provide some additional insight.
The problem regarding static initialization is known as static initialization order fiasco:
In short, suppose you have two static objects x and y which exist in
separate source files, say x.cpp and y.cpp. Suppose further that the
initialization for the y object (typically the y object’s constructor)
calls some method on the x object.
So if you have another translation unit using your constants, you have a rather good chance that your program will not work. Sometimes it is the order the files were linked together, some plattforms even define it in the doc (I think Solaris is one example here).
The problem also applies to builtin types such as int. The example from the FAQ is:
#include <iostream>
int f(); // forward declaration
int g(); // forward declaration
int x = f();
int y = g();
int f()
{
std::cout << "using 'y' (which is " << y << ")\n";
return 3*y + 7;
}
int g()
{
std::cout << "initializing 'y'\n";
return 5;
}
int main() {
std::cout << x << std::endl << y << std::endl;
return 0;
}
If you run this example, the output is:
using 'y' (which is 0)
initializing 'y'
So y first gets zero-initialized and then constant initialization (?) happens.
The solution is the Construct On First Use Idiom:
The basic idea of the Construct On First Use Idiom is to wrap your
static object inside a function.
Static loca objects are constructed the first time the control flow reaches their declaration.
I came across the following rule (3.7.1/2 N3797):
If a variable with static storage duration has initialization or a
destructor with side effects, it shall not be eliminated even if it
appears to be unused, except that a class object or its copy/move may
be eliminated as specified in 12.8.
Is it true that, if we declare a variable with static storage duration, having constructor or destructor with side-effect, then the memory will allocate for that variable even if it is unused?
In particular, I'm trying to experiment with the following code:
#include <csignal>
#include <iostream>
#include <cstdlib>
using std::cout;
struct data_member
{
data_member(){ cout << "data_member\n"; }
~data_member(){ cout << "~data_member\n"; }
};
struct Y
{
static data_member m;
Y(){ cout << "Y\n"; }
~Y(){ cout << "~Y\n"; }
};
Y y;
int main()
{
}
IdeOne
Why does data_member object not constructed in the example?
Well, that's what the standard says, so ... yes.
As always, there's the as-if rule, allowing a compiler to do practically anything, as long as the net effect is the same. I could imagine that if the class is huge data-wise, but the constructor and destructor (and anybody else) do not access that data, the compiler could just call the constructor and destructor in the proper order, without leaving space for the unused data.
As for your example, you're declaring Y::m, but you never define it. So it doesn't exist. If you tried to access it, you'd get a linker error.
I have a global object which is declared before main function and a static object inside main.
Which one of these uses (or do both?) static initialization?
I've heard that A obj1 is also called static; why is that?
class A { ... };
A obj1;
int main()
{
static A obj2;
}
obj1 has static storage. It will be initialized when the program starts.
obj2 also has static storage because you said so. It will be initialized when main() executes the first time.
My first doubt is how precise the question is. If static initailization is used with the technical meaning in the standard, it represents zero-initialization and initialization of a POD type with constant expressions for objects with storage duration, compared with dynamic initialization where the initialization of the object with static storage duration is initialized somehow else.
For an illustrative example:
// at namespace level
int f();
int a; // 1 static: zero initialization
int b = 10; // 2 static: initialization from constant expression
int c = f(); // 3 static (zero initialization)
// 5 followed by dynamic (result of call to f())
int d = 20; // 4 static: initialization
int f() { return d; }
int main() {}
The number indicates the order of execution of each initialization step. The reason why the compiler considers both static and dynamic initialization is that it sets an order on the execution of the initialization. static initialization is executed before dynamic initialization, and all statically initialized objects are guaranteed to have their value before any dynamic initialization starts. That is, the standard guarantees that even if d appears after c in the previous program, the value of c is guaranteed to be 20.
For objects that require dynamic initialization, they are (conceptually) zero initialized during static initailization, so during the lifetime of c, it is first set to 0, and later reset to f() (or 20 in this program). The conceptually is due to the fact that if the compiler can infer the final value that the variable will get, it can optimize the dynamic initialization away and just perform plain static initialization with that final value (in the above code, the compiler could detect that c is going to be dynamically initialized to 20, and decide to transform it into static initialization like int c = 20; in a conforming implementation. In that case steps 3 and 5 in the code above would be merged into a single step 3.
In the case of a local variable with static storage duration, the standard does not use the terms static/dynamic initialization, but the descriptions require the same behavior from the program. Local variables with static duration are zero-initialized or POD initialized with constant expressions when (or before) the block is entered for the first time (as with static initialization), while for the rest of the initializations are performed the first time that control passes over through it's declaration.
Focusing on C++ only, you also need to consider initialization of static members.
It concerns static members of a class.
For example, when you have:
class A {
// declaration of i as a static member of class A
static int i;
};
// initialization of static member outside class A
int A::i = 42;
Both of these variables are static:
obj1 is a non-local static variable which will be initialized when the program starts.
obj2 is a local static variable which will be initialised when the function is first called.
Scott Meyers in his book "Effective C++" recommends accessing local static variables through functions as opposed to using non-local static variables. Doing so avoids the so called static initialization order problem where one variable can reference another which may not have been initialized yet because of the arbitrary order in which initialization occurs.
If I have a class called Test ::
class Test
{
static std::vector<int> staticVector;
};
when does staticVector get constructed and when does it get destructed ?
Is it with the instantiation of the first object of Test class, or just like regular static variables ?
Just to clarify, this question came to my mind after reading Concepts of Programming Languages (Sebesta Ch-5.4.3.1) and it says ::
Note that when the static modifier
appears in the declaration of a
variable in a class definition in C++,
Java and C#, it has nothing to do with
the lifetime of the variable. In that
context, it means the variable is a
class variable, rather than an
instance variable. The multiple use
of a reserved word can be confusing
particularly to those learning the
language.
did you understand? :(
I want to write some text about initializaton too, which i can later link to.
First the list of possibilities.
Namespace Static
Class Static
Local Static
Namespace Static
There are two initialization methods. static (intended to happen at compile time) and dynamic (intended to happen at runtime) initialization.
Static Initialization happens before any dynamic initialization, disregarding of translation unit relations.
Dynamic Initiaization is ordered in a translation unit, while there is no particular order in static initialization. Objects of namespace scope of the same translation unit are dynamically initialized in the order in which their definition appears.
POD type objects that are initialized with constant expressions are statically initialized. Their value can be relied on by any object's dynamic initialization, disregarding of translation unit relations.
If the initialization throws an exception, std::terminate is called.
Examples:
The following program prints A(1) A(2)
struct A {
A(int n) { std::printf(" A(%d) ", n); }
};
A a(1);
A b(2);
And the following, based on the same class, prints A(2) A(1)
extern A a;
A b(2);
A a(1);
Let's pretend there is a translation unit where msg is defined as the following
char const *msg = "abc";
Then the following prints abc. Note that p receives dynamic initialization. But because the static initialization (char const* is a POD type, and "abc" is an address constant expression) of msg happens before that, this is fine, and msg is guaranteed to be correctly initialized.
extern const char *msg;
struct P { P() { std::printf("%s", msg); } };
P p;
Dynamic initialization of an object is not required to happen before main at all costs. The initialization must happen before the first use of an object or function of its translation unit, though. This is important for dynamic loadable libraries.
Class Static
Behave like namespace statics.
There is a bug-report on whether the compiler is allowed to initialize class statics on the first use of a function or object of its translation unit too (after main). The wording in the Standard currently only allows this for namespace scope objects - but it seems it intends to allow this for class scope objects too. Read Objects of Namespace Scope.
For class statics that are member of templates the rule is that they are only initialized if they are ever used. Not using them will not yield to an initialization. Note that in any case, initialization will happen like explained above. Initialization will not be delayed because it's a member of a template.
Local Static
For local statics, special rules happen.
POD type objects initialized with constant expression are initialized before their block in which they are defined is entered.
Other local static objects are initialized at the first time control passes through their definition. Initialization is not considered to be complete when an exception is thrown. The initialization will be tried again the next time.
Example: The following program prints 0 1:
struct C {
C(int n) {
if(n == 0)
throw n;
this->n = n;
}
int n;
};
int f(int n) {
static C c(n);
return c.n;
}
int main() {
try {
f(0);
} catch(int n) {
std::cout << n << " ";
}
f(1); // initializes successfully
std::cout << f(2);
}
In all the above cases, in certain limited cases, for some objects that are not required to be initialized statically, the compiler can statically initialize it, instead of dynamically initializing it. This is a tricky issue, see this answer for a more detailed example.
Also note that the order of destruction is the exact order of the completion of construction of the objects. This is a common and happens in all sort of situations in C++, including in destructing temporaries.
Exactly like regular static (global) variables.
It gets constructed at the same time the global variables get constructed and destructed along with the globals as well.
Simply speaking:
A static member variable is constructed when the global variables are constructed. The construction order of global variables is not defined, but it happens before the main-function is entered.
Destruction happens when global variables are destroyed.
Global variables are destroyed in the reversed order they were constructed; after exiting the main-function.
Regards,
Ovanes
P.S.: I suggest to take a look at C++-Standard, which explains (defines) how and when global or static member variables are constructed or destructed.
P.P.S.: Your code only declares a static member variable, but does not initialize it. To initialize it you must write in one of the compilation units:
std::vector Test::staticVector;
or
std::vector Test::staticVector=std::vector(/* ctor params here */);
Some specific VC++ information in case that's what you're using:
Static class variables construction occurs at same time as other static/global variables.
In windows, the CRT startup function is responsible for this construction.
This is the actual entry point of most programs you compile (it is the function which calls your Main/Winmain function).
In addition, it is responsible for initializing the entire C runtime support (for example you need it to use malloc).
The order of construction is undefined, however when using the microsoft VC compiler the order of construction for basic types will be OK, for example it is legal and safe to write
statics.h:
... MyClass declaration ...
static const int a;
static int b;
static int ar[];
}
statics.cpp:
const int MyClass::a = 2;
int MyClass::b = a+3;
int MyClass::ar[a] = {1,2}
I'm quite confident that globally declared variables get allocated (and initialized, if applicable) at program start time.
int globalgarbage;
unsigned int anumber = 42;
But what about static ones defined within a function?
void doSomething()
{
static bool globalish = true;
// ...
}
When is the space for globalish allocated? I'm guessing when the program starts. But does it get initialized then too? Or is it initialized when doSomething() is first called?
I was curious about this so I wrote the following test program and compiled it with g++ version 4.1.2.
include <iostream>
#include <string>
using namespace std;
class test
{
public:
test(const char *name)
: _name(name)
{
cout << _name << " created" << endl;
}
~test()
{
cout << _name << " destroyed" << endl;
}
string _name;
};
test t("global variable");
void f()
{
static test t("static variable");
test t2("Local variable");
cout << "Function executed" << endl;
}
int main()
{
test t("local to main");
cout << "Program start" << endl;
f();
cout << "Program end" << endl;
return 0;
}
The results were not what I expected. The constructor for the static object was not called until the first time the function was called. Here is the output:
global variable created
local to main created
Program start
static variable created
Local variable created
Function executed
Local variable destroyed
Program end
local to main destroyed
static variable destroyed
global variable destroyed
Some relevant verbiage from C++ Standard:
3.6.2 Initialization of non-local objects [basic.start.init]
1
The storage for objects with static storage
duration (basic.stc.static) shall be zero-initialized (dcl.init)
before any other initialization takes place. Objects of
POD types (basic.types) with static storage duration
initialized with constant expressions (expr.const) shall be
initialized before any dynamic initialization takes place.
Objects of namespace scope with static storage duration defined in
the same translation unit and dynamically initialized shall be
initialized in the order in which their definition appears in
the translation unit. [Note: dcl.init.aggr describes the
order in which aggregate members are initialized. The
initialization of local static objects is described in stmt.dcl. ]
[more text below adding more liberties for compiler writers]
6.7 Declaration statement [stmt.dcl]
...
4
The zero-initialization (dcl.init) of all local objects with
static storage duration (basic.stc.static) is performed before
any other initialization takes place. A local object of
POD type (basic.types) with static storage duration
initialized with constant-expressions is initialized before its
block is first entered. An implementation is permitted to perform
early initialization of other local objects with static storage
duration under the same conditions that an implementation is
permitted to statically initialize an object with static storage
duration in namespace scope (basic.start.init). Otherwise such
an object is initialized the first time control passes through its
declaration; such an object is considered initialized upon the
completion of its initialization. If the initialization exits by
throwing an exception, the initialization is not complete, so it will
be tried again the next time control enters the declaration. If control re-enters the declaration (recursively) while the object is being
initialized, the behavior is undefined. [Example:
int foo(int i)
{
static int s = foo(2*i); // recursive call - undefined
return i+1;
}
--end example]
5
The destructor for a local object with static storage duration will
be executed if and only if the variable was constructed.
[Note: basic.start.term describes the order in which local
objects with static storage duration are destroyed. ]
The memory for all static variables is allocated at program load. But local static variables are created and initialized the first time they are used, not at program start up. There's some good reading about that, and statics in general, here. In general I think some of these issues depend on the implementation, especially if you want to know where in memory this stuff will be located.
The compiler will allocate static variable(s) defined in a function foo at program load, however the compiler will also add some additional instructions (machine code) to your function foo so that the first time it is invoked this additional code will initialize the static variable (e.g. invoking the constructor, if applicable).
#Adam: This behind the scenes injection of code by the compiler is the reason for the result you saw.
I try to test again code from Adam Pierce and added two more cases: static variable in class and POD type. My compiler is g++ 4.8.1, in Windows OS(MinGW-32).
Result is static variable in class is treated same with global variable. Its constructor will be called before enter main function.
Conclusion (for g++, Windows environment):
Global variable and static member in class: constructor is called before enter main function (1).
Local static variable: constructor is only called when execution reaches its declaration at first time.
If Local static variable is POD type, then it is also initialized before enter main function (1).
Example for POD type: static int number = 10;
(1): The correct state should be: "before any function from the same translation unit is called". However, for simple, as in example below, then it is main function.
#include <iostream>
#include <string>
using namespace std;
class test
{
public:
test(const char *name)
: _name(name)
{
cout << _name << " created" << endl;
}
~test()
{
cout << _name << " destroyed" << endl;
}
string _name;
static test t; // static member
};
test test::t("static in class");
test t("global variable");
void f()
{
static test t("static variable");
static int num = 10 ; // POD type, init before enter main function
test t2("Local variable");
cout << "Function executed" << endl;
}
int main()
{
test t("local to main");
cout << "Program start" << endl;
f();
cout << "Program end" << endl;
return 0;
}
result:
static in class created
global variable created
local to main created
Program start
static variable created
Local variable created
Function executed
Local variable destroyed
Program end
local to main destroyed
static variable destroyed
global variable destroyed
static in class destroyed
Anybody tested in Linux env ?
Or is it initialized when doSomething() is first called?
Yes, it is. This, among other things, lets you initialize globally-accessed data structures when it is appropriate, for example inside try/catch blocks. E.g. instead of
int foo = init(); // bad if init() throws something
int main() {
try {
...
}
catch(...){
...
}
}
you can write
int& foo() {
static int myfoo = init();
return myfoo;
}
and use it inside the try/catch block. On the first call, the variable will be initialized. Then, on the first and next calls, its value will be returned (by reference).
Static variables are allocated inside a code segment -- they are part of the executable image, and so are mapped in already initialized.
Static variables within function scope are treated the same, the scoping is purely a language level construct.
For this reason you are guaranteed that a static variable will be initialized to 0 (unless you specify something else) rather than an undefined value.
There are some other facets to initialization you can take advantage off -- for example shared segments allow different instances of your executable running at once to access the same static variables.
In C++ (globally scoped) static objects have their constructors called as part of the program start up, under the control of the C runtime library. Under Visual C++ at least the order that objects are initialized in can be controlled by the init_seg pragma.
In the following code it prints Initial = 4 which is the value of static_x as it is implemented in the compiling time.
int func(int x)
{
static int static_x = 4;
static_x = x;
printf ("Address = 0x%x",&static_x ); // prints 0x40a010
return static_x;
}
int main()
{
int x = 8;
uint32_t *ptr = (uint32_t *)(0x40a010); // static_x location
printf ("Initial = %d\n",*ptr);
func(x);
return 0;
}