Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ static initialization vs __attribute__((constructor))

Example:

struct Foo { Foo() { printf("foo\n"); } };
static Foo foo;

__attribute__((constructor)) static void _bar() { printf("bar\n"); }

Is it deterministic wether foo or bar is printed first?

(I hope and would expect that constructors of static objects are always executed first but not sure and GCCs doc about the constructor attribute doesn't say anything about it.)

like image 643
Albert Avatar asked Dec 08 '11 15:12

Albert


2 Answers

foo will be printed first, as the objects are initialized in the order of their declarations. Run and see:

  • Ideone online demo

By the way, __attribute__((constructor)) is not Standard C++. It is GCC's extension. So the behavior of your program depends on how GCC has defined it. In short, it is implementation-defined, according to it foo is printed first.

The doc says,

The constructor attribute causes the function to be called automatically before execution enters main (). Similarly, the destructor attribute causes the function to be called automatically after main () has completed or exit () has been called. Functions with these attributes are useful for initializing data that will be used implicitly during the execution of the program.

You may provide an optional integer priority to control the order in which constructor and destructor functions are run. A constructor with a smaller priority number runs before a constructor with a larger priority number; the opposite relationship holds for destructors. So, if you have a constructor that allocates a resource and a destructor that deallocates the same resource, both functions typically have the same priority. The priorities for constructor and destructor functions are the same as those specified for namespace-scope C++ objects (see C++ Attributes).

I think the text in bold implies, the objects are initialized in the order of their declarations, as I said before, which is pretty much confirmed by online demo also.

I guess you would also like to read this:

  • 7.7 C++-Specific Variable, Function, and Type Attributes

If you want to control/alter the initialization order, you can use init_priority attribute, providing priority. Taken from the page:

Some_Class  A  __attribute__ ((init_priority (2000)));
Some_Class  B  __attribute__ ((init_priority (543)));

Here, B is initialized before A.

like image 158
Nawaz Avatar answered Oct 11 '22 03:10

Nawaz


It isn't defined by the standard, but here is my experiment https://github.com/SanSanch5/static-initialization-order-example

The output on gcc-9:

CFoo constructed
dll constructor
CBaz constructed
CBar constructed
foo
dll destructor
CBar destructed
CBaz destructed
CFoo destructed

The output on clang-12:

CFoo constructed
dll constructor
CBaz constructed
CBar constructed
foo
CBar destructed
dll destructor
CBaz destructed
CFoo destructed

So, the dll constructor is being called before static initialization, but as you can see, the destruction is slightly different on gcc and clang. But initializing a static object inside a dll constructor experimentally proves its destruction after the other static objects. But it's non-standard so not reliable.

like image 42
Alexander Pakhomov Avatar answered Oct 11 '22 04:10

Alexander Pakhomov