Questions tagged [static-initialization]

Questions regarding initialization code of static members

292 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
  • 830
  • 6
  • 21
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
  • 17,895
  • 16
  • 55
  • 102
14
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
  • 343
  • 1
  • 3
  • 8
14
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
  • 303
  • 1
  • 3
  • 11
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
  • 58,778
  • 7
  • 120
  • 175
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
  • 22,978
  • 1
  • 62
  • 101
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
  • 11,736
  • 17
  • 80
  • 126
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,295
  • 1
  • 14
  • 23
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
  • 211
  • 3
  • 7
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
  • 10,729
  • 30
  • 55
11
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,347
  • 2
  • 22
  • 33
11
votes
7 answers

Static variable initialization?

I want to know why exactly static variables in C, C++ and Java are initialized by zero by default? And why this is not true for local variables?
Sachin Chourasiya
  • 18,417
  • 31
  • 80
  • 95
11
votes
2 answers

Application-wide configuration of Lambdaj FinalClassArgumentCreators. Where and how to do it?

We have a problem with configuring lambdaj to work with Joda Time. Since LocalDate is a final class, Lambdaj needs to be initialized like following: (see bug 70) public class LocalDateArgumentCreator implements FinalClassArgumentCreator
RJo
  • 12,543
  • 5
  • 26
  • 63
10
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
  • 647
  • 5
  • 16
1
2
3
19 20