help-gplusplus
[Top][All Lists]
Advanced

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

Re: cannot initialize static pointer class member in __attribute__ ((co


From: jeremy barrett
Subject: Re: cannot initialize static pointer class member in __attribute__ ((constructor))
Date: Fri, 3 Oct 2008 11:53:18 -0700 (PDT)
User-agent: G2/1.0

On Oct 3, 12:20 am, Paul Pluzhnikov <address@hidden> wrote:
> jeremy  barrett <address@hidden> writes:
>
> > int* A::x = new int(5);
>
> Note that 'A::x' is initialized to a non-constant value. That means
> 'gcc' has to initialize it dynamically. In effect, 'gcc' writes a
> new function (called static_initialization_and_destruction()),
> which is called via exactly the same mechanism as your initAx().
>
> The order of static_init...() and initAx() is not specified,
> gcc-3.3.3 for Linux/x86 calls initAx() after static_init...(),
> while gcc-4.3.0 calls them in reverse order.

Thanks for this insight.  I discovered (anecdotally) the same thing
last night, while trying to reproduce this error on a different
platform (gcc-4.1.3 on Linux/x86 at work, gcc-4.0.1 on Mac/x86 at
home).

((snip))

>
> It should be enlightening for you to compile the following separate
> snippets into assembly and understand the result:
>
> int x = 5;     // compile-time constant
>
> int x;
> int *px = &x;  // link-time constant
>
> int foo();
> int x = foo(); // runtime intialization required

I'll perform this exercise. Thanks.

>
> Finally, it is entirely pointless to provide initAx() and initBx().
> If you want to initialize A::x and B::x to some values, just do
> so directly:

Here's the point.  A and B are very generic classes that are included
in a number of different libraries I've built.  So the static
initializers set x to something suitably generic for use in standard
cases.

In order keep data appropriately hidden (and save myself some
bookkeeping), I add this constructor function to various of the shared
libraries to initialize x to something meaningful (conceptual zero)
for the given application/environment.

Any (portable) ideas about how I can accomplish my goal? I understand
that __attribute__ ((constructor)) is not portable, but is there at
least a method that will work with the generic condition:

#ifdef __GNUC__
//some brilliant idea
#endif

?

Thanks!
jeremy


reply via email to

[Prev in Thread] Current Thread [Next in Thread]