Re: Method to specify initialization order across translation units?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



>> Are there any methods to specify initialization order across
>> translation units? If so, what is it (or are they)?
>
> init_priority (priority)
>
>     In Standard C++, objects defined at namespace scope are guaranteed
> to be initialized in an order in strict accordance with that of their
> definitions in a given translation unit. No guarantee is made for
> initializations across translation units. However, GNU C++ allows
> users to control the order of initialization of objects defined at
> namespace scope with the init_priority attribute by specifying a
> relative priority, a constant integral expression currently bounded
> between 101 and 65535 inclusive. Lower numbers indicate a higher
> priority.
>
>     In the following example, A would normally be created before B,
> but the init_priority attribute reverses that order:
>
>               Some_Class  A  __attribute__ ((init_priority (2000)));
>               Some_Class  B  __attribute__ ((init_priority (543)));
>
>     Note that the particular values of priority do not matter; only
> their relative ordering.

Thanks again Andrew.

Forgive my ignorance... what init_priority should I use? I selected a
base priority of 250, and then specified the order of 5 file scope
objects. The objects were 3 std::strings and 2 C++ class objects.
That's the extent of file scope C++ objects in this library.

My thinking was 250 will give the standard runtime and other required
libraries to initialize first. Then, initialize the five objects of
interest. These are objects which have implicit dependencies that's we
can't seem to express in C++ Then initialize "don't care" values,
which don't appear to have a dependency with special needs.

After the initialization of the 5 C++ objects, I specified 1
[non-class] function as a constructor with a priority of 300.

Combined, it resulted in the issue with the POD type initialization
(like a bool is no longer initialized when used), and issues with
libstdc++. So 250/300 is not the magic number...

Thanks in advance.

==16604== Conditional jump or move depends on uninitialised value(s)
==16604==    at 0x4F37F4E: std::ostreambuf_iterator<char,
std::char_traits<char> > std::num_put<char,
std::ostreambuf_iterator<char, std::char_traits<char> >
>::_M_insert_int<long>(std::ostreambuf_iterator<char,
std::char_traits<char> >, std::ios_base&, char, long) const (in
/usr/lib64/libstdc++.so.6.0.21)
==16604==    by 0x4F381FF: std::num_put<char,
std::ostreambuf_iterator<char, std::char_traits<char> >
>::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >,
std::ios_base&, char, bool) const (in /usr/lib64/libstdc++.so.6.0.21)
==16604==    by 0x4F44BDA: std::ostream&
std::ostream::_M_insert<bool>(bool) (in
/usr/lib64/libstdc++.so.6.0.21)



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux