c++ - Is static object guaranteed to be initialized -


i trying learn initialization of static objects. static initialization seems pretty straight forward, assuming understand constant expressions , constexpr. dynamic initialization seems quite bit more tricky.

[basic.start.init]/4

it implementation-defined whether dynamic initialization of non-local variable static storage duration done before first statement of main. if initialization deferred point in time after first statement of main, shall occur before first odr-use (3.2) of function or variable defined in same translation unit variable initialized.

footnote 34

a non-local variable static storage duration having initialization side-effects must initialized if not odr-used (3.2, 3.7.1).

[basic.start.init]/5

it implementation-defined whether dynamic initialization of non-local variable static or thread storage duration done before first statement of initial function of thread. if initialization deferred point in time after first statement of initial function of thread, shall occur before first odr-use (3.2) of variable thread storage duration defined in same translation unit variable initialized.

i assume "the initial function of thread" refers main, , not threads started std::thread.

h1.h

#ifndef h1_h_ #define h1_h_  extern int count;  #endif 

tu1.cpp

#include "h1.h"  struct s {    s()    {       ++count;    } };  s s; 

tu2.cpp

#include "h1.h"  int main(int argc, char *argv[]) {    return count; } 

tu3.cpp

#include "h1.h"  int count; 

so, if compiler defers dynamic initialization, seems footnote 34 states s must initialized @ point. since there no other variables dynamic initialization in translation unit, there no other variable odr-use force initialization of variables in tu1. @ point s guaranteed have been initialized?

is main guaranteed return 1? also, there way change program such no longer guaranteed return 1? alternatively, if isn't guaranteed, there way change program such becomes guaranteed?


i broke code definition of s in different translation unit main. avoids question of whether main odr used. given s object in translation unit, guaranteed main return 1?

i think wording there describe happen in dynamic loaded libraries, without explicitly naming them.

to summarize how interpret it: non-local variable static storage duration , dynamic initialization will:

  1. be initialized before first odr-use of in translation unit;
  2. possibly, before starting main, possibly after it.

i interpret footnote 34 (but remember footnotes not normative):

when in tu ord-used, every non-local variable static storage duration having initialization side-effects must initialized, variables not odr-used.

so, if there tu nothing ord-used, dynamic initializations may not happen.

example

h1.h

extern int count; struct s {     s(); }; 

h1.cpp

#include "h1.h"  int count; s::s() {    ++count; } 

h2.cpp

#include "h1.h" s s; 

main.cpp

#include "h1.h" #include <stdio.h> int main() {     printf("%d\n", count); } 

this print 0 or 1: since in tu h2 never odr-used, unspecified when code initialization of s done, if @ all.

naturally, sane compilers initialize s before main, surely print 1:

$ g++ main.cpp h2.cpp h1.cpp -o test1 $ ./test1 1 

now, imagine h2.cpp in shared library:

$ g++ -shared -fpic h2.cpp -o h2.so 

and main file now:

main2.cpp

#include "h1.h" #include <dlfcn.h> #include <stdio.h>  int main() {     printf("%d\n", count);     dlopen("./h2.so", rtld_now);     printf("%d\n", count);     return 0; } 

compile , run:

$ g++ -shared -fpic h2.cpp -o h2.so $ g++ -rdynamic main.cpp h1.cpp -ldl -o test2 $ ./test2 0 1 

see? initialization of s has been delayed! nice part is impossible reference in dynamic loaded library without first loading it, , loading trigger dynamic initialization. well.

if think using dlopen cheating, remember there compilers support delay loading shared libraries (vc++ example), system call load library generated automatically compiler first time needed.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -