Topic: Question on order of initiallization of statics


Author: kanze@us-es.sel.de (James Kanze)
Date: 27 Apr 93 18:37:32
Raw View
In article <1993Apr22.155529.11566@taumet.com> steve@taumet.com (Steve
Clamage) writes:

|> >Can anyone offer any advice on the order of initiallization of statics?

|> >Is anything at all guaranteed.  What would be really useful is if I could
|> >rely on simple assignments of built in types being done before assignments
|> >involving executing functions.  For example, could I rely on

|> The ARM requires that non-local static objects are initialized
|> before any reference to any function in the module.  As a matter of
|> practicality, most implementations (other than dynamic libraries
|> or code overlays) initialize all static objects before main() begins.

|> The ARM further requires that static objects with a default
|> initialization of zero are initialized before any dynamic initialization
|> (as with expressions).  As a matter of practicality, implementations
|> generally initialize all objects of simple type with constant values
|> at program load time, followed by dynamic initialization.

|> Finally, non-local static objects within one module are initialized
|> in the order in which they are defined in the module.

Can anyone cite the passage in the ARM where this is stated.  It is
true for all implementations I know of, and I had always believed that
it was a requirement in the ARM.  But the other day, when asked by
someone to point out in the ARM where this was stated, I was unable to
find it.  (I was also unable to find it in the current working draft
of the standards committee, using grep.)
--
James Kanze                             email: kanze@us-es.sel.de
GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
Conseils en informatique industrielle --
                   -- Beratung in industrieller Datenverarbeitung




Author: steve@taumet.com (Steve Clamage)
Date: Sat, 24 Apr 1993 22:16:45 GMT
Raw View
maxtal@physics.su.OZ.AU (John Max Skaller) writes:

|In article <1993Apr22.155529.11566@taumet.com> steve@taumet.com (Steve Clamage) writes:

|>Finally, non-local static objects within one module are initialized
|>in the order in which they are defined in the module.

| Is that true, or are they initialised on first
|execution of the initialiser:

| func(int a) {
|  static int first = a;
| };

Please note: "non-local statics".  Your example is a local static,
which is different.  A non-local static would be one at file scope.
--

Steve Clamage, TauMetric Corp, steve@taumet.com




Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Tue, 27 Apr 1993 10:57:39 GMT
Raw View
In article <1993Apr24.221645.17241@taumet.com> steve@taumet.com (Steve Clamage) writes:
>maxtal@physics.su.OZ.AU (John Max Skaller) writes:
>
>|In article <1993Apr22.155529.11566@taumet.com> steve@taumet.com (Steve Clamage) writes:
>
>|>Finally, non-local static objects within one module are initialized
>|>in the order in which they are defined in the module.
>
>| Is that true, or are they initialised on first
>|execution of the initialiser:
>
>| func(int a) {
>|  static int first = a;
>| };
>
>Please note: "non-local statics".  Your example is a local static,
>which is different.  A non-local static would be one at file scope.

 Absolutely right. My apologies.

--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,      CSERVE:10236.1703
        6 MacKay St ASHFIELD,     Mem: SA IT/9/22,SC22/WG21
        NSW 2131, AUSTRALIA




Author: srlncnc@gopher.dosli.govt.nz (Chris Crook)
Date: Thu, 22 Apr 1993 01:58:45 GMT
Raw View
Can anyone offer any advice on the order of initiallization of statics?

Is anything at all guaranteed.  What would be really useful is if I could
rely on simple assignments of built in types being done before assignments
involving executing functions.  For example, could I rely on

static anyClass *ptr = 0;

being assigned before any initiallization such as

static anyClass value;

(which involves a constructor)?

This appears to be the case with Borland C++, though I could not describe
my testing so far as conclusive.  But is it portable?

My apologies if this is obvious - I do not have access to a copy of the
ARM or a similar reference.

Many thanks in advance

Chris Crook




Author: steve@taumet.com (Steve Clamage)
Date: Thu, 22 Apr 1993 15:55:29 GMT
Raw View
srlncnc@gopher.dosli.govt.nz (Chris Crook) writes:

>Can anyone offer any advice on the order of initiallization of statics?

>Is anything at all guaranteed.  What would be really useful is if I could
>rely on simple assignments of built in types being done before assignments
>involving executing functions.  For example, could I rely on

The ARM requires that non-local static objects are initialized
before any reference to any function in the module.  As a matter of
practicality, most implementations (other than dynamic libraries
or code overlays) initialize all static objects before main() begins.

The ARM further requires that static objects with a default
initialization of zero are initialized before any dynamic initialization
(as with expressions).  As a matter of practicality, implementations
generally initialize all objects of simple type with constant values
at program load time, followed by dynamic initialization.

Finally, non-local static objects within one module are initialized
in the order in which they are defined in the module.
--

Steve Clamage, TauMetric Corp, steve@taumet.com




Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Sat, 24 Apr 1993 14:06:29 GMT
Raw View
In article <1993Apr22.155529.11566@taumet.com> steve@taumet.com (Steve Clamage) writes:
>srlncnc@gopher.dosli.govt.nz (Chris Crook) writes:
>
>>Can anyone offer any advice on the order of initiallization of statics?
>
>>Is anything at all guaranteed.  What would be really useful is if I could
>>rely on simple assignments of built in types being done before assignments
>>involving executing functions.  For example, could I rely on
>
>The ARM requires that non-local static objects are initialized
>before any reference to any function in the module.

 However the ARM requirement is incorrect because no
system could possibly make this guarrantee, and the ARM itself
disusses these issues.

 In particular, a dynamic initialisation by a function
call is allowed.

 It would perhaps be better to just say that
all the variables in a module are dynamically initialised
in order before main is called. (order across modules
is indeterminate)

>The ARM further requires that static objects with a default
>initialization of zero are initialized before any dynamic initialization
>(as with expressions).

 The ARM is somewhat unclear, as is the Working Paper.
It just isnt clear that initialisations to compile time constants
of *other* than zero are, or are not, carried out before or
after dynamic initialisations.

>As a matter of practicality, implementations
>generally initialize all objects of simple type with constant values
>at program load time, followed by dynamic initialization.

 Which would probably be a better specification.
>
>Finally, non-local static objects within one module are initialized
>in the order in which they are defined in the module.

 Is that true, or are they initialised on first
execution of the initialiser:

 func(int a) {
  static int first = a;
 };


--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,      CSERVE:10236.1703
        6 MacKay St ASHFIELD,     Mem: SA IT/9/22,SC22/WG21
        NSW 2131, AUSTRALIA