static-initialization

g++, static initialization and -nostdlib

非 Y 不嫁゛ 提交于 2019-11-28 23:34:42
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 can call manually? This is what I tried: g++ -o test.o -c -fno-use-cxa-atexit test.cc # has _start (entry point) # that calls _init and _main as -o crti.o crti.s # has _init in section .init as -o crtn.o crtn.s g++ -o test ./crti.o test.o -nodefaultlibs -nostartfiles ./crtn.o -nodefaultlibs alone includes static initialization code and call, but

Static initializer runs after the constructor, why?

戏子无情 提交于 2019-11-28 21:13:25
I have 2 classes: Class A: public class A { static B b = new B(); static { System.out.println("A static block"); } public A() { System.out.println("A constructor"); } } Class B: public class B { static { System.out.println("B static block"); new A(); } public B() { System.out.println("B constructor"); } } I create a Main class which just creates new A: public class Main { public static void main(String[] args) { new A(); } } The output I get is: B static block A constructor B constructor A static block A constructor As you can see, the constructor of A is invoked before its static initializer.

How to mitigate user-facing API Effect of shared members in templated classes?

末鹿安然 提交于 2019-11-28 14:01:36
问题 Let's say I have a type of lookup table which I can build for a given integer: class FooLookupTable { ... public: FooLookupTable(int radix) { ... } }; Then there's a class whose template parameter is that same integer, and whose constructor initializes a member instance of this lookup table: template <int radix> class Foo { ... private: FooLookupTable table; public: Foo () : FooLookupTable (radix) { ... } }; Throughout my code I instantiate these with various values of radix: int main() { ...

Are there any guarantees in JLS about order of execution static initialization blocks?

跟風遠走 提交于 2019-11-28 02:16:30
I wonder if it's reliable to use a construction like: private static final Map<String, String> engMessages; private static final Map<String, String> rusMessages; static { engMessages = new HashMap<String, String> () {{ put ("msgname", "value"); }}; rusMessages = new HashMap<String, String> () {{ put ("msgname", "значение"); }}; } private static Map<String, String> msgSource; static { msgSource = engMessages; } public static String msg (String msgName) { return msgSource.get (msgName); } Is there a possibility that I'll get NullPointerException because msgSource initialization block will be

Java Legal Forward Referencing

别等时光非礼了梦想. 提交于 2019-11-27 23:39:53
问题 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[] args) { System.out.println(x); } } 回答1: The above code you have is perfectly legal Java. In Java, static fields are initialized as follows: first, all fields are set to the default for their type (0, false , or null ), and then initialized in the order in which

How to force gcc to link unreferenced, static C++ objects from a library

微笑、不失礼 提交于 2019-11-27 20:06:09
I'm using a C++ library that can be built as either a shared or a static library. This library uses a factory technique, where static objects register themselves when the program starts and the static objects get created. This works fine as long as the shared library is used. When the static version is used, none of the static objects get included into the final program (because they aren't referenced directly) and thus their functionality isn't available. Is there a way to force gcc to include all static objects from a library when linking? The library is Open Source and I could modify it, if

Static initializer runs after the constructor, why?

三世轮回 提交于 2019-11-27 13:49:27
问题 I have 2 classes: Class A: public class A { static B b = new B(); static { System.out.println("A static block"); } public A() { System.out.println("A constructor"); } } Class B: public class B { static { System.out.println("B static block"); new A(); } public B() { System.out.println("B constructor"); } } I create a Main class which just creates new A: public class Main { public static void main(String[] args) { new A(); } } The output I get is: B static block A constructor B constructor A

Java: When is a static initialization block useful?

 ̄綄美尐妖づ 提交于 2019-11-27 10:02:46
What's the difference between initialization within a static block: public class staticTest { static String s; static int n; static double d; static { s = "I'm static"; n = 500; d = 4000.0001; } ... And individual static initialization: public class staticTest { static String s = "I'm static"; static int n = 500; static double d = 4000.0001; .... A static initialization blocks allows more complex initialization, for example using conditionals: static double a; static { if (SomeCondition) { a = 0; } else { a = 1; } } Or when more than just construction is required: when using a builder to

static initialization in interface

梦想与她 提交于 2019-11-27 07:45:26
When I tried to write something like this: public interface MyInterface { static { System.out.println("Hello!"); } } the compiler could not compile it. But when I wrote something like this: interface MyInterface { Integer iconst = Integer.valueOf(1); } and decompiled it, I saw static initialization: public interface MyInterface{ public static final java.lang.Integer i; static {}; Code: 0: iconst_1 1: invokestatic #1; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 4: putstatic #2; //Field i:Ljava/lang/Integer; 7: return } Could you please explain this behavior to me? You can have

Prevent static initialization order “fiasco”, C++

回眸只為那壹抹淺笑 提交于 2019-11-27 07:41:11
问题 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 maintaining the creation order of variables. But this seems to me a rude workaround. So my question is, is there any modern, more pattern oriented way to prevent this "fiasco" but to wrap the "static stuff" into functions??? 回答1: The modern, more pattern