C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Static data initialization

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Albrecht Fritzsche
Guest





PostPosted: Fri Jun 10, 2005 10:25 am    Post subject: Static data initialization Reply with quote



Is it guaranteed that all static data is initialized *prior* to the
begin of main()? Can anyone please point me to the places in the
standard where these guarantees are spelled out?

Thanks for any help
Ali
PS I am not referring to the "initialization order of static data in
different translation units" problem.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
alerma
Guest





PostPosted: Sat Jun 11, 2005 6:29 pm    Post subject: Re: Static data initialization Reply with quote



Global objects are initialized at program startup. Local objects
declared as static are initialized the first time their declarations
are encountered in the program flow.
MSDN, C++ Language Reference


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
James Kanze
Guest





PostPosted: Sun Jun 12, 2005 4:59 pm    Post subject: Re: Static data initialization Reply with quote



Albrecht Fritzsche wrote:
Quote:
Is it guaranteed that all static data is initialized *prior*
to the begin of main()?

Formally, no. In practice, this is the case for all known
implementations, provided the object files are linked
statically; it is also the basis of several well published
programming techniques, so it is doubtful that any
implementation would dare not give this guarantee.

Quote:
Can anyone please point me to the places in the standard where
these guarantees are spelled out?

The section covering the question is §3.6.2: Initialization of
non-local objects. The key paragraph with regards to your
question is the paragraph 3:

It is implementation-defined whether or not the dynamic
initialization of an object of namespace scope is done
before the first statement of main. If the initialization
is deferred to some point in time after the first statement
of main, it shall occur before the first use of any function
or object defined in the same translation unit as the
object to be initialized.

Note that the second sentence is in fact impossible to guarantee
in the face of circular dependancies.

I believe that the committee is actually considering some formal
support for dynamic linking. If this is adopted, this paragraph
will probably end up considerably reworked. In the meantime, at
least in the implementations I'm familiar with: objects in
statically linked object files are constructed befor entering
main; objects in dynamically linked object files are constructed
before returning from the link request (dlopen in Unix).

--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.