the static-order-fiasco refers to the problem of interdependent static initializations in different compilation units in C and C++. The ordering of startup code is up to the linker and thus often unpredictable.
Questions tagged [static-order-fiasco]
42 questions
72
votes
6 answers
C++ static initialization order
When I use static variables in C++, I often end up wanting to initialize one variable passing another to its constructor. In other words, I want to create static instances that depend on each other.
Within a single .cpp or .h file this is not a…
![](../../users/profiles/74612.webp)
Dimitri C.
- 20,224
- 19
- 78
- 99
62
votes
12 answers
Finding C++ static initialization order problems
We've run into some problems with the static initialization order fiasco, and I'm looking for ways to comb through a whole lot of code to find possible occurrences. Any suggestions on how to do this efficiently?
Edit: I'm getting some good answers…
![](../../users/profiles/10077.webp)
Fred Larson
- 56,061
- 15
- 106
- 157
25
votes
4 answers
static initialization order fiasco
I was reading about SIOF from a book and it gave an example :
//file1.cpp
extern int y;
int x=y+1;
//file2.cpp
extern int x;
int y=x+1;
Now My question is :
In above code, will following things happen ?
while compiling file1.cpp, compiler…
![](../../users/profiles/317283.webp)
Happy Mittal
- 3,477
- 11
- 41
- 58
17
votes
2 answers
Nifty/Schwarz counter, standard compliant?
I had a discussion this morning with a colleague about static variable initialization order. He mentioned the Nifty/Schwarz counter and I'm (sort of) puzzled. I understand how it works, but I'm not sure if this is, technically speaking, standard…
![](../../users/profiles/313063.webp)
André Caron
- 41,491
- 10
- 58
- 117
13
votes
2 answers
Initializing qt resources embedded in static library
I have next situation: I need to create widget in standalone static library, which then will be linked with final application (visual c++ 9.0, qt 4.5).
This static widget library contains some resources (icons), and consist of a several .cpp files…
![](../../users/profiles/170050.webp)
cybevnm
- 2,506
- 4
- 28
- 33
10
votes
11 answers
Does Java have the static order initialisation fiasco?
A recent question here had the following code (well, similar to this) to implement a singleton without synchronisation.
public class Singleton {
private Singleton() {}
private static class SingletonHolder {
private static final…
![](../../users/profiles/14860.webp)
paxdiablo
- 772,407
- 210
- 1,477
- 1,841
10
votes
5 answers
Static initialization order fiasco
In his "Thinking in C++" (Chapter 10) Eckel describes a technique that was pioneered by Jerry Schwarz to solve the fiasco.
He says that if we want to initialize x to 100 and y to 200 and share them among all translation units, we create an…
![](../../users/profiles/635780.webp)
Dello
- 267
- 4
- 9
10
votes
3 answers
Prevent static initialization order "fiasco", C++
Once I was reading an awesome C++ FAQ (It is really good!!) and read the topic about how to prevent the static initialization order "fiasco". So the author advises to wrap the static variables into functions, thus to prevent the "fiasco" by…
![](../../users/profiles/1856429.webp)
Eduard Rostomyan
- 5,154
- 1
- 23
- 56
8
votes
1 answer
std::thread::hardware_concurrency and static initialization
May this global function suffer from static initialization fiasco?
template
void ParallelFor(int iIni,int iFin,TFn Fn)
{
static const unsigned int NThread= std::thread::hardware_concurrency();
// ...
}
![](../../users/profiles/6350078.webp)
metalfox
- 5,299
- 1
- 15
- 38
7
votes
3 answers
Double initialization of a static STL container in a C++ library
There are a few good questions and answers here around the "static initialization order fiasco", but I seem to have hit against yet another expression of it, specially ugly because it does not crash but looses and leaks data.
I have a custom C++…
![](../../users/profiles/501336.webp)
gatopeich
- 2,169
- 24
- 23
6
votes
2 answers
Does this code produce Undefined Behavior or it is merely Unspecified Behavior?
Lets say that we have two compilation units as follows:
// a.cpp
extern int value2;
int value1 = value2 + 10;
// b.cpp
extern int value1;
int value2 = value1 + 10;
When I tried it on VC2010, it initializes value1 and value2 to zero first. aren't…
![](../../users/profiles/127893.webp)
AraK
- 87,541
- 35
- 171
- 230
6
votes
1 answer
Order of initialization and destruction of block-scope static vs. namespace-scope thread_local in main thread
I'm trying to understand the sequencing rules for initialization and destruction of namespace-scope and block-scope objects with static storage duration and thread-local storage duration in the context of the main thread. Consider these two…
![](../../users/profiles/1639256.webp)
Oktalist
- 13,098
- 1
- 38
- 56
5
votes
2 answers
Is the order of file-level static variables always the same within a given translation unit?
I have a program split up into two source files:
example.cpp
#include
class A {
public:
A(int x) {
::std::cout << "In A(" << x << ")\n";
}
};
static A first(1);
static A second(2);
example__main.cpp
int main(int argc,…
![](../../users/profiles/167958.webp)
Omnifarious
- 50,447
- 15
- 117
- 181
5
votes
1 answer
Static order initialization fiasco, iostream and C++11
According to C++11 specification:
The results of including in a translation unit shall be as if defined an instance of ios_base::Init with static storage duration. Similarly, the entire program shall behave as if there were…
![](../../users/profiles/1060159.webp)
marmistrz
- 5,296
- 9
- 31
- 84
3
votes
1 answer
Can C++20 `constinit` waive the need for nifty counter idiom?
C++20 introduced constinit to avoid static initialization order fiasco.
Can constinit waive the need for the nifty counter idiom (e.g. for initialization of std::cout)?
![](../../users/profiles/2085626.webp)
Amir Kirsh
- 8,021
- 22
- 43