public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* init_priority and initialization of POD types
@ 2015-08-12 18:19 Jeffrey Walton
  2015-08-18 20:18 ` Martin Sebor
  0 siblings, 1 reply; 2+ messages in thread
From: Jeffrey Walton @ 2015-08-12 18:19 UTC (permalink / raw)
  To: gcc-help

I'm tracking down some Valgrind findings. I believe I am seeing the following:

    // a.cpp
    bool g_flag = false;

    // b.cpp
    SomeObject g_object1 __attribute__ ((init_priority(150)));

    // c.cpp
    // Uses global flag from a.cpp, and SomeObject from b.cpp
    SomeOtherObject g_object2;

When I run the program under Valgrind, I'm catching an uninitialized
finding related to the global flag from a.cpp when g_object2 is used.

According to the man page on init_priority
(https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html):

    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.

I assumed the object initialization came after POD initialization in a
translation unit, but it appears I am wrong. In fact, the man page
does not discuss the scenario.

What is the relationship among C++ objects with an init_priority
attribute and other POD types across translation units?

Thanks in advance.

Jeff

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: init_priority and initialization of POD types
  2015-08-12 18:19 init_priority and initialization of POD types Jeffrey Walton
@ 2015-08-18 20:18 ` Martin Sebor
  0 siblings, 0 replies; 2+ messages in thread
From: Martin Sebor @ 2015-08-18 20:18 UTC (permalink / raw)
  To: noloader, gcc-help

On 08/12/2015 12:19 PM, Jeffrey Walton wrote:
> I'm tracking down some Valgrind findings. I believe I am seeing the following:
>
>      // a.cpp
>      bool g_flag = false;
>
>      // b.cpp
>      SomeObject g_object1 __attribute__ ((init_priority(150)));
>
>      // c.cpp
>      // Uses global flag from a.cpp, and SomeObject from b.cpp
>      SomeOtherObject g_object2;
>
> When I run the program under Valgrind, I'm catching an uninitialized
> finding related to the global flag from a.cpp when g_object2 is used.
>
> According to the man page on init_priority
> (https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html):
>
>      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.
>
> I assumed the object initialization came after POD initialization in a
> translation unit, but it appears I am wrong. In fact, the man page
> does not discuss the scenario.
>
> What is the relationship among C++ objects with an init_priority
> attribute and other POD types across translation units?

POD objects with static storage duration with constant initializers
are initialized statically, at program load time, before dynamic
initialization starts. They're not subject to init_priority which
applies to dynamically initialized objects (i.e., whose ctors run
at program startup). I can't imagine under what circumstances
Valgrind would complain that a statically initialized POD object
is uninitialized.

Martin

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-08-18 20:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-12 18:19 init_priority and initialization of POD types Jeffrey Walton
2015-08-18 20:18 ` Martin Sebor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).