[Background: Due to my training, I am rather sensitive to global and static initialization.]
I think Puneet's quotation of the standard of is almost sufficient except that he is quoting Bjarne's book (an excellent reference) rather than the official standard. I am concerned that gcc is a particular implementation and subject to (mis-)interpretation [although, I do think it is less so than others due to the open source nature and multitude of eyes]. So I delved into the standard and I now agree with Puneet after obtaining the following.
Standard ISO/IEC 14882:2003(E) states
Section 3.7.1 paragraph 3 (page 46)
The keyword static can be used to declare a local variable with static storage duration. [Note: 6.7
describes the initialization of local static variables; 3.6.3 describes the destruction of local static
variables. ]
Section 3.6.2 paragraph 1 (page 44):
Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place. Zero-initialization and initialization with a constant expression are collectively called static initialization; all other initialization is dynamic initialization. Objects of POD types (3.9) with static storage duration initialized with constant expressions (5.19) shall be initialized before any dynamic initialization takes place. Objects with static storage duration defined in namespace scope in the same translation unit and dynamically initialized shall be initialized in the order in which their definition appears in the translation unit. [Note: 8.5.1 describes the order in which aggregate members are initialized. The initialization of local static objects is described in 6.7. ]
Section 6.7 paragraph 4 (page 101)
The zero-initialization (8.5) of all local objects with static storage duration (3.7.1) is performed before any
other initialization takes place. A local object of POD type (3.9) with static storage duration initialized with
constant-expressions is initialized before its block is first entered. An implementation is permitted to perform
early initialization of other local objects with static storage duration under the same conditions that an
implementation is permitted to statically initialize an object with static storage duration in namespace scope
(3.6.2). Otherwise such an object is initialized the first time control passes through its declaration; such an
object is considered initialized upon the completion of its initialization. If the initialization exits by throwing
an exception, the initialization is not complete, so it will be tried again the next time control enters the
declaration. If control re-enters the declaration (recursively) while the object is being initialized, the behavior
is undefined. [Example:
int foo(int i)
{
static int s = foo(2*i); // recursive call – undefined
return i+1;
}
—end example]
I believe what we are trying to do qualifies as dynamic initialization and therefore not subject to early initialization.
------------------------------------------------------
David C Black, XtremeEDA ESL Practice Leader
http://www.Xtreme-EDA.com
(Consulting, Services & Training for all your ESL, verification and DFT needs)
Voice: 512.850.4322 Skype:dcblack FAX: 888.467.4609
On Sep 14, 2010, at 4:24 AM, john.aynsley@doulos.com wrote:
> I tried Philipp's code on gcc and it worked, which confirms Puneet's story.
>
> John A
>
> -----owner-systemc-p1666-technical@eda.org wrote: -----
> To: David C Black <dcblack@xtreme-eda.com>
> From: Puneet Goel
> Sent by: owner-systemc-p1666-technical@eda.org
> Date: 09/14/2010 04:56AM
> Cc: "Philipp A. Hartmann" <philipp.hartmann@offis.de>, SystemC P1666 Technical <systemc-p1666-technical@eda.org>
> Subject: Re: Next priority: Maximum value of simulation time
>
> Greetings
>
> > I don't think a local static will work because it will get created before
> > main() if I recall the correct semantics of C++.
>
> Section 7.1.2 Stroustrup The C++ Programming Language
>
> If a local variable is declared static, a single, statically allocated object will be used to represent that variable in all calls of the function. It will be initialized only the first time the thread of execution reaches its definition.
>
> In our case (as suggested by Phillipp) that would happen only when the first call to sc_max_time() is made.
>
> Regards
> - Puneet
>
>
> --
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is
> believed to be clean.
-- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.Received on Tue Sep 14 04:08:57 2010
This archive was generated by hypermail 2.1.8 : Tue Sep 14 2010 - 04:08:59 PDT