On Oct 3, 12:20 am, Paul Pluzhnikov <[EMAIL PROTECTED]> wrote:
> jeremy  barrett <[EMAIL PROTECTED]> 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
_______________________________________________
help-gplusplus mailing list
help-gplusplus@gnu.org
http://lists.gnu.org/mailman/listinfo/help-gplusplus

Reply via email to