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; 4+ 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] 4+ 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; 4+ 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] 4+ 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; 4+ 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] 4+ messages in thread
* [Bug c++/37667] Initialization of global variables and functions with constructor attribute.
[not found] <bug-37667-4@http.gcc.gnu.org/bugzilla/>
@ 2021-12-25 1:33 ` pinskia at gcc dot gnu.org
0 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-25 1:33 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=37667
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution|--- |INVALID
--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Michael Uleysky from comment #2)
> 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)));
Because you have the wrong order, lower number is a higher priority which means
101/Init will always be first.
From
https://gcc.gnu.org/onlinedocs/gcc-11.2.0/gcc/Common-Function-Attributes.html#index-constructor-function-attribute:
"However, at present, the order in which constructors for C++ objects with
static storage duration and functions decorated with attribute constructor are
invoked is unspecified. In mixed declarations, attribute init_priority can be
used to impose a specific ordering."
"A constructor with a smaller priority number runs before a constructor with a
larger priority"
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-12-25 1:33 UTC | newest]
Thread overview: 4+ 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
[not found] <bug-37667-4@http.gcc.gnu.org/bugzilla/>
2021-12-25 1:33 ` pinskia at gcc dot gnu.org
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).