static-initialization

Static variable initialization?

拜拜、爱过 提交于 2019-11-27 04:33:38
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? Paweł Hajdan Why the static variables are deterministically initialized and local variables aren't? See how the static variables are implemented. The memory for them is allocated at link time, and the initial value for them is also provided at link time. There is no runtime overhead. On the other hand, the memory for local variables is allocated at run time. The stack has to grow. You don't know what was there before. If you want, you can clear that

How to force a static member to be initialized?

别等时光非礼了梦想. 提交于 2019-11-27 01:02:02
Consider this example code: template<class D> char register_(){ return D::get_dummy(); // static function } template<class D> struct Foo{ static char const dummy; }; template<class D> char const Foo<D>::dummy = register_<D>(); struct Bar : Foo<Bar> { static char const get_dummy() { return 42; } }; ( Also on Ideone .) I'd expect dummy to get initialized as soon as there is a concrete instantiation of Foo , which I have with Bar . This question (and the standard quote at the end) explained pretty clear, why that's not happening. [...] in particular, the initialization (and any associated side

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

左心房为你撑大大i 提交于 2019-11-26 22:10:20
问题 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

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

爷,独闯天下 提交于 2019-11-26 20:09:52
问题 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

Trouble launching CUDA kernels from static initialization code

随声附和 提交于 2019-11-26 19:07:29
I have a class that calls a kernel in its constructor, as follows: "ScalarField.h" #include <iostream> void ERROR_CHECK(cudaError_t err,const char * msg) { if(err!=cudaSuccess) { std::cout << msg << " : " << cudaGetErrorString(err) << std::endl; std::exit(-1); } } class ScalarField { public: float* array; int dimension; ScalarField(int dim): dimension(dim) { std::cout << "Scalar Field" << std::endl; ERROR_CHECK(cudaMalloc(&array, dim*sizeof(float)),"cudaMalloc"); } }; "classA.h" #include "ScalarField.h" static __global__ void KernelSetScalarField(ScalarField v) { int index = threadIdx.x +

Java: When is a static initialization block useful?

我怕爱的太早我们不能终老 提交于 2019-11-26 17:53:25
问题 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; .... 回答1: A static initialization blocks allows more complex initialization, for example using conditionals: static double a; static { if (SomeCondition)

Why isn't a qualified static final variable allowed in a static initialization block?

天大地大妈咪最大 提交于 2019-11-26 16:13:29
Case 1 class Program { static final int var; static { Program.var = 8; // Compilation error } public static void main(String[] args) { int i; i = Program.var; System.out.println(Program.var); } } Case 2 class Program { static final int var; static { var = 8; //OK } public static void main(String[] args) { System.out.println(Program.var); } } Why does Case 1 cause a compilation error? The JLS holds the answer (note the bold statement): Similarly, every blank final variable must be assigned at most once; it must be definitely unassigned when an assignment to it occurs. Such an assignment is

static initialization in interface

扶醉桌前 提交于 2019-11-26 13:48:07
问题 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; /

Static variable initialization?

孤人 提交于 2019-11-26 11:16:23
问题 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? 回答1: Why the static variables are deterministically initialized and local variables aren't? See how the static variables are implemented. The memory for them is allocated at link time, and the initial value for them is also provided at link time. There is no runtime overhead. On the other hand, the memory for local variables is allocated at run time.

How to force a static member to be initialized?

本小妞迷上赌 提交于 2019-11-26 08:16:26
问题 Consider this example code: template<class D> char register_(){ return D::get_dummy(); // static function } template<class D> struct Foo{ static char const dummy; }; template<class D> char const Foo<D>::dummy = register_<D>(); struct Bar : Foo<Bar> { static char const get_dummy() { return 42; } }; (Also on Ideone.) I\'d expect dummy to get initialized as soon as there is a concrete instantiation of Foo , which I have with Bar . This question (and the standard quote at the end) explained