Questions tagged [static-initialization]

Questions regarding initialization code of static members

292 questions
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…
10
votes
2 answers

Destruction order of statically initialized, non-literal objects

A recent question drew my attention to the way that constexpr has changed in C++14. The new feature is that a non-local variable with static storage duration may be initialized in the static initialization phase if its initializer consists of a…
Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
10
votes
1 answer

c++ static template members initialization issue

gcc 4.5.1, SuSE Linux i686 Suppose we have following code: template class B { public: B() {std::cout << "B()" << std::endl;} }; template class A { public: static B static_var; }; template
Ribtoks
  • 5,820
  • 1
  • 19
  • 29
9
votes
1 answer

Static pointer to object initialization thread-safety

In C++11, the following is thread-safe: void someFunc() { static MyObject object; } But what about void someFunc() { static MyObject *ptr = new MyObject(); } Is this then thread-safe or not? As it was mentioned in the comments by @Nawaz,…
Vladimir
  • 279
  • 1
  • 9
9
votes
4 answers

__attribute__((constructor)) call order confusion

The answer here demonstrates that __attribute__((constructor)) is not called after static initialization, it is called in the declaration order. Then, what is the purpose of it, if it is not guaranteed to be called when all data is initialized? We…
queen3
  • 14,883
  • 8
  • 54
  • 114
8
votes
1 answer

Static initialization by JVM

language: java version: 12.0.2 String source code as follows: /* @implNote * The actual value for this field is injected by JVM. The static * initialization block is used to set the value here to communicate * that this static final field is not…
Xiu
  • 170
  • 6
8
votes
1 answer

std::atexit ordering when called from a global object's constructor

cppreference says about std::atexit : The functions may be called concurrently with the destruction of the objects with static storage duration and with each other, maintaining the guarantee that if registration of A was sequenced-before the…
8
votes
3 answers

Class with private constructor and static array of itself

Sorry if title is confusing, I couldn't find an easy way to write it in a simple sentence. Anyways, the issue I'm facing: // header: class SomeThing { private: SomeThing() {} // <- so users of this class can't come up // …
Gábor Buella
  • 1,632
  • 13
  • 19
8
votes
1 answer

Trouble launching CUDA kernels from static initialization code

I have a class that calls a kernel in its constructor, as follows: "ScalarField.h" #include void ERROR_CHECK(cudaError_t err,const char * msg) { if(err!=cudaSuccess) { std::cout << msg << " : " <<…
Noel
  • 640
  • 5
  • 14
8
votes
2 answers

Is it safe to create and use vectors during static initialization?

I have C++ code which declares static-lifetime variables which are initialized by function calls. The called function constructs a vector instance and calls its push_back method. Is the code risking doom via the C++ static initialization order…
aecolley
  • 1,705
  • 10
  • 9
8
votes
3 answers

Can threads be safely created during static initialization?

At some point I remember reading that threads can't be safely created until the first line of main(), because compilers insert special code to make threading work that runs during static initialization time. So if you have a global object that…
7
votes
2 answers

Is what constitutes a failed initialization of block-scope static or thread storage duration variables underspecified?

After answering this question and not finding a satisfying answer in the standard paper, I started wondering. The standard states the following w.r.t. initialization of mentioned variables: §6.7 [stmt.dcl] p4 [...] Otherwise such a variable is…
Xeo
  • 123,374
  • 44
  • 277
  • 381
7
votes
3 answers

Is it possible to ensure that a function is only called during the 'static initialization' step

I was wondering if it is possible to ensure that a function is only called during the static initialization step of a program? As an example, say that I have some singleton class that contains a std::map object and exposes the insert and at methods…
extiam
  • 95
  • 6
7
votes
2 answers

Is initialization of local static function-object thread-safe?

The following two functions produce different assemblies, which tells me they're different. Can someone tell me in what way they are different? And is the function local static variable initialization in func2 thread-safe or not? If the answer…
zeroes00
  • 511
  • 3
  • 16
6
votes
1 answer

How Do Zero-Initialization, Static-Initialization, and Value-Initialization Differ?

Ben Voigt has pointed out here that: Zero initialization is one of the steps of static initialization. But you're right that you can't blindly substitute the latter (tag), since zero initialization is also performed for value initialization.…
Jonathan Mee
  • 35,107
  • 16
  • 95
  • 241
1 2
3
19 20