Questions tagged [static-initialization]

Questions regarding initialization code of static members

325 questions
11
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
  • 730
  • 6
  • 15
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
  • 20,805
  • 32
  • 86
  • 99
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
  • 15,631
  • 5
  • 32
  • 63
10
votes
1 answer

How do I suppress "has no attribute" errors from mypy when assigning a new attribute to a function?

I often use the following idiom for static initialization: def compute_answer() -> int: if compute_answer.ret is None: # Do stuff that only happens the first time compute_answer.ret = 42 return…
Vic
  • 487
  • 8
  • 17
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
  • 6,634
  • 1
  • 25
  • 37
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
  • 11
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
  • 15,333
  • 8
  • 64
  • 119
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…
chachay
  • 340
  • 2
  • 8
8
votes
1 answer

Static inline members initialization order

A well known problem in C++ is the static initialization order fiasco. Is it still considered a problem when one use C++17 static inline members? Here an example where a static inline member is used in two different translation units (a.cpp and…
Tarquiscani
  • 649
  • 7
  • 20
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,840
  • 14
  • 22
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,973
  • 11
  • 10
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
  • 129,499
  • 52
  • 291
  • 397
7
votes
2 answers

Static final variable initialization (in Java) incorrect during Kotlin CI Tests

I manage an open source project and have a user reporting a situation which I think is impossible according to Java's order of initialization of static variables in classes. The value of a static final class variable is incorrect, apparently…
Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
7
votes
1 answer

Why are const qualified variables accepted as initializers on gcc?

When compiling this code in latest verson of gcc (or clang) with -std=c17 -pedantic-errors -Wall -Wextra static const int y = 1; static int x = y; then I get no compiler diagnostic message even though I'm fairly sure that this is not valid C but a…
Lundin
  • 195,001
  • 40
  • 254
  • 396
1 2
3
21 22