Questions tagged [static-initialization]

Questions regarding initialization code of static members

325 questions
17
votes
2 answers

When is initialization of global constants with external linkage safe from static initialization order fiasco?

Consider the following example: tt.h declares a global constant with external linkages extern int g_TRAGIC; tt.cpp defines g_TRAGIC as follows const int g_TRAGIC = 0xF001; my.cpp wants to use it to define its own global constant const int g_MAGIC…
Lothar
  • 860
  • 6
  • 21
17
votes
3 answers

Static Initialization in Go?

I'm currently working on the Go Lang tutorial, but ran into problem with one of the exercises: https://tour.golang.org/methods/23 The exercise has me implement a ROT13 cipher. I decided to implement the cipher using a map from a byte to its rotated…
jlhawn
  • 373
  • 1
  • 3
  • 8
17
votes
5 answers

java static initialization with inheritance

public class Main { public static void main(String[] args) { System.out.println(B.x); } } class A { public static String x = "x"; } class B extends A { static { System.out.print("Inside B."); } } Question: Why…
Taras Koval
  • 173
  • 1
  • 4
16
votes
2 answers

Initializing circular data in C. Is this valid C code according to any standard?

I wanted to see if I could initialize a global variable to point to itself: #include struct foo { struct foo *a, *b; } x = { &x, &x }; int main() { printf("&x = %p, x.a = %p, x.b = %p\n", &x, x.a, x.b); return 0; } This code…
Matt
  • 21,026
  • 18
  • 63
  • 115
15
votes
1 answer

g++, static initialization and -nostdlib

Compiling / linking with -nostdlib seems to prevent static initialization, even if I add my own crti.s and crtn.s with .init/.fini sections. Are there workarounds to make g++ generate static initialization code that is inserted in .init or that I…
Thomas
  • 323
  • 1
  • 3
  • 11
14
votes
4 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…
13
votes
1 answer

Finding out whether static initialization is over

The abridged problem (Y) Suppose you need to know, from within a function, whether this function has been called as part of the initialization of a static object, or not. Is there a standard or platform-specific way to do so? The backstory (X) I'm…
Quentin
  • 62,093
  • 7
  • 131
  • 191
12
votes
3 answers

static const array gets initialized dynamically in MSVC?

We have a table we'd like to initialize statically, however MSVC (2015.1, and older versions too) generates a dynamic initializer instead. Here's the simplified code demonstrating the issue: #define idaapi __stdcall #define MAXSTR 1024 typedef int…
Igor Skochinsky
  • 24,629
  • 2
  • 72
  • 109
12
votes
1 answer

Android 6.0 (Marshmallow) static initialization exception on getDeclaredField() in svg-android library

I'm having some serious problem with this code, from svg-android: public class ParserHelper { private static final Field STRING_CHARS; static { try { STRING_CHARS = String.class.getDeclaredField("value"); //<-- exception here …
Seraphim's
  • 12,559
  • 20
  • 88
  • 129
12
votes
4 answers

Why should I not initialize static variable in header?

So, let's say I have a header like this: #ifndef BASECLASS_H #define BASECLASS_H class BaseClass { public: static int getX(){return x;} private: static int x; }; int BaseClass::x = 10; #endif I have heard many times that…
user3496846
  • 1,627
  • 3
  • 16
  • 28
12
votes
2 answers

non-deferred static member initialization for templates in gcc?

Does gcc have any guarantees about static member initialization timing, especially regarding template classes? I want to know if I can get a hard guarantee that static members (PWrap_T::p_s) will be initialized before main(), when classes are…
Jeff
  • 3,475
  • 4
  • 26
  • 35
12
votes
2 answers

Java Legal Forward Referencing

Is the following code the case of legal forward referencing? if yes why? public class MyClass { private static int x = getValue(); private static int y = 5; private static int getValue() { return y; } public static void main(String[]…
Vibhor
  • 229
  • 1
  • 3
  • 8
11
votes
3 answers

Initialize static std::map with non copyable value in a uniformed inline initialization

I'd like to initialize a static std::map where the value is not copyable. I'll call my class ValueClass. ValueClass has an std::unique_ptr as private member and I even ensure that ValueClass is not copyable by extending non_copyable that looks like…
U. Bulle
  • 1,075
  • 9
  • 20
11
votes
7 answers

Can I access a static local while it is being constructed in C++?

Static locals are guaranteed to be instantiated at first use by the C++ standard. However, I'm wondering what happens if I access a static local object while it is beeing constructed. I assume that this is UB. But what are the best practices to…
king_nak
  • 11,313
  • 33
  • 58
11
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
  • 464,522
  • 92
  • 875
  • 1,084
1
2
3
21 22