public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/37667]  New: Initialization of global variables and functions with constructor attribute.
@ 2008-09-28  0:44 uleysky at gmail dot com
  2008-09-28 18:58 ` [Bug c++/37667] " pinskia at gcc dot gnu dot org
  2008-09-29  2:08 ` uleysky at gmail dot com
  0 siblings, 2 replies; 3+ messages in thread
From: uleysky at gmail dot com @ 2008-09-28  0:44 UTC (permalink / raw)
  To: gcc-bugs

The test code

#include <stdlib.h>
class Test
{
 public:
 int t;
 Test():t(1) {}
};

Test ex_t;

void Init() __attribute__ ((constructor));
void Init() { if(ex_t.t==0) abort(); }

int main(void) { return 0; }

aborted if compiled with gcc 4.3.2. If compiled with gcc 4.1.0 or 3.3.6 it
proceed normally. As I understand, in first case the ex_t constructor called
after function Init, but in second case the ex_t constructor called before
function Init. I not found in documentation what case is right, so I think this
is a bug. In my opinion, constructors of global variables must have more
priority than __attribute__ ((constructor)) functions.

Compilation command is simple: g++ -o test test.cpp


-- 
           Summary: Initialization of global variables and functions with
                    constructor attribute.
           Product: gcc
           Version: 4.3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: uleysky at gmail dot com
 GCC build triplet: x86_64-unknown-linux
  GCC host triplet: x86_64-unknown-linux
GCC target triplet: x86_64-unknown-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37667


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

* [Bug c++/37667] Initialization of global variables and functions with constructor attribute.
  2008-09-28  0:44 [Bug c++/37667] New: Initialization of global variables and functions with constructor attribute uleysky at gmail dot com
@ 2008-09-28 18:58 ` pinskia at gcc dot gnu dot org
  2008-09-29  2:08 ` uleysky at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-09-28 18:58 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2008-09-28 18:57 -------
This works on the trunk at -O0 but fails at -O2.

I think this is undefined as the priority are the same.

You should use init_priority and set a priority for the constructor.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37667


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

* [Bug c++/37667] Initialization of global variables and functions with constructor attribute.
  2008-09-28  0:44 [Bug c++/37667] New: Initialization of global variables and functions with constructor attribute uleysky at gmail dot com
  2008-09-28 18:58 ` [Bug c++/37667] " pinskia at gcc dot gnu dot org
@ 2008-09-29  2:08 ` uleysky at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: uleysky at gmail dot com @ 2008-09-29  2:08 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from uleysky at gmail dot com  2008-09-29 02:07 -------
The priority must not be same. The only method to interact constructor function
with main is global variables. It is no reason to initialize global variables
after constructor function. Anyway,

Test ex_t __attribute__ ((init_priority(102)));
void Init() __attribute__ ((constructor(101)));

have no effect.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37667


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

end of thread, other threads:[~2008-09-29  2:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-09-28  0:44 [Bug c++/37667] New: Initialization of global variables and functions with constructor attribute uleysky at gmail dot com
2008-09-28 18:58 ` [Bug c++/37667] " pinskia at gcc dot gnu dot org
2008-09-29  2:08 ` uleysky at gmail dot com

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).