Questions tagged [static-order-fiasco]

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.

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…
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…
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…
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…
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…
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…
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…
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…
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(); // ... }
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++…
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…
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…
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,…
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…
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)?
Amir Kirsh
  • 8,021
  • 22
  • 43
1
2 3