1

From Programming Language Pragmatics, by Scott

Object lifetimes generally correspond to one of three principal storage allocation mechanisms, used to manage the object’s space:

  1. Static objects are given an absolute address that is retained throughout the program’s execution.

  2. Stack objects are allocated and deallocated in last-in, first-out order, usually in conjunction with subroutine calls and returns.

  3. Heap objects may be allocated and deallocated at arbitrary times. They require a more general (and expensive) storage management algorithm.

For example, in C, static objects must be initialized with constant expressions (expressions which can be evaluated at compile time).

I am not sure whether it is the case in other languages and even what other languages also have static objects.

In general, must static objects be initialized? When initialized, must they be initialized with expressions which can be evaluated at compile time?

By initialization, I mean either explicit or implicit (i.e. automatically done by language implementation), as opposed to uninitailziation. So to rephrase my question: generally, can static objects be left uninitialized by either programs or compilers?

Thanks.

Tim
  • 1
  • 141
  • 372
  • 590
  • Short answer is 'no'. However they probably should to avoid uninitialized issues later in the program. – Serge Sep 15 '17 at 22:55
  • Not exactly true, a `static` variable is default to be initialized to 0 (or equivalent), a `non static` variable should be initialized actually, otherwise would be some garbage value in your memory stack. – James Maa Sep 15 '17 at 22:57

1 Answers1

3

A static variable will be initialized to "zero" automatically, unless you explicitly initialize it.

Other than that and the life-time or linkage part, it's no different than any other variable, which means you can initialize it the same way you initialize any other variable.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks. By initialization, I mean either explicit or implicit, as opposed to uninitailziation. So do you mean static objects must be initialized? – Tim Sep 15 '17 at 22:56
  • @Tim No, they will be automatically initialized by the "compiler" before `main` is called, unless you do it explicitly. – Some programmer dude Sep 15 '17 at 22:57
  • Let me rephrase my question: generally, can static objects be left uninitialized by either programs or compilers? – Tim Sep 15 '17 at 23:10
  • @Tim In that case, no. They will always be initialized (unless you have a buggy compiler or startup routine). See e.g. [this old answer](https://stackoverflow.com/a/13251173/440558) with quotes from the standard. – Some programmer dude Sep 15 '17 at 23:16
  • As far as you know, is this the case in other languages? – Tim Sep 15 '17 at 23:22
  • @Tim - Memory for global variables ("static storage duration") will commonly be zeroed by the operating system so that you cannot peek on the data used by previous programs. C documents this as a requirement but the effect will be the same for other languages, whether they require it or not. – Bo Persson Sep 15 '17 at 23:50
  • @Bo: Thanks. How about static variables local to functions? – Tim Sep 16 '17 at 00:06
  • @Tim Even in that case a `static` variable will not be left uninitialized. – Some programmer dude Sep 16 '17 at 08:15
  • @Someprogrammerdude From https://stackoverflow.com/a/41125798 "if the value isn't known at compile time for a `static const` at function scope, it might silently make your function (a very small bit) slower, since it has to initialize the value at runtime the first time the function is called. " So does it mean that static objects may be initialized at runtime, if the value of its initializer is not known at compile time? – Tim Sep 16 '17 at 13:37
  • @Tim The "may" here is that it may be initialized at run-time *or* at compile-time. It will still be initialized though. As I said before, *all* `static` variables will be initialized, one way or another. – Some programmer dude Sep 16 '17 at 13:50
  • @Someprogrammerdude I just noticed the other post is about C++. Maybe C still requires static objects be initialized at compile time, while C++ relaxes it to allow runtime initialization? – Tim Sep 16 '17 at 14:06
  • @Tim I should probably have asked a long time ago, but *why* are you asking? Is it just plain curiosity? Or is it some specific problem related to `static` variable initialization that lead you to ask this question? Perhaps you should be asking about *that* problem instead then? – Some programmer dude Sep 16 '17 at 14:12
  • @Someprogrammerdude It is just my question when reading the book Programming Language Pragmatics and reflecting on my past experience with C a while ago. If you need specific problem to answer a question, or no specific problem makes you uncomfortable, I apologize. – Tim Sep 16 '17 at 14:15