home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!mcsun!news.funet.fi!ajk.tele.fi!funic!nokia.fi!newshost!girod
- From: girod@kilohp.kiloapo.ts.tele.nokia.fi (Marc Girod)
- Subject: Re: Static global initialization outside of main() legal?
- In-Reply-To: nagle@netcom.com's message of Thu, 31 Dec 1992 19:11:23 GMT
- Message-ID: <GIROD.93Jan4122311@kilohp.kiloapo.ts.tele.nokia.fi>
- Sender: usenet@noknic.nokia.fi (USENET at noknic)
- Nntp-Posting-Host: kilohp.ts.tele.nokia.fi
- Reply-To: marc.girod@ntc.nokia.com
- Organization: /user/girod/.organization
- References: <34727@sales.GBA.NYU.EDU> <1992Dec31.191123.7222@netcom.com>
- Date: Mon, 4 Jan 1993 10:23:11 GMT
- Lines: 49
-
- wlee@sales.GBA.NYU.EDU (Wai-Shing Lee) writes:
- wlee> I want to initialize a static global at run-time but I don't want to
- wlee> have to have a programmer using my modules to have to call some init() funciton
- wlee> at the begining of thier main().
-
- This is a problem I have met already. I find it most recommendable to
- find a way to insure that needed static data is transparently
- initialised before use, without any explicit call of the user. This
- should definitely be well (better!) supported by the language.
-
- >>>>> On Thu, 31 Dec 1992 19:11:23 GMT, nagle@netcom.com (John Nagle) said:
- John> You're working in a very messy area of C++. The order in which
- John> things happen during static initialization isn't the same in different
- John> compilers. The language rules arein section r3.4 of "The C++ Programming
- John> Language, Second Edition", and you should read them if you want to do this
- John> sort of thing.
-
- Things are not so bad as John states! The most important is
- guaranteed: that "the initialisation [...] in a translation unit is
- done before the first use of any function or object defined in that
- translation unit". There remains the "pathologic" case of mutual
- dependencies, dealt with in the annotation part of the ARM for 3.4,
- ... and the treatment of "virtual constructors" through the
- declaration of meta-class objects, the precise class of which should
- be kept unknown to the rest of the world...
-
- John> In most implementations, static initializers are run before
- John> "main" is called.
-
- But not in all, as unfortunately allowed by the language (e.g. g++).
-
- John> For initializers that don't call functions, this
- John> is enough to get the static variables initialized properly. But
- John> initializers that call functions can create serious problems, especially
- John> if the functions have side effects, are in a different compilation unit
- John> (file), or depend on other static variables being initialized. The order
- John> in which things get initialized varies from compiler to compiler and
- John> sometimes from compile to compile, so you can't depend on this at all.
-
- Well, there is no surprise: you should not rely on such order, but if
- you may "force" it explicitly, e.g. with the technique of "nifty
- counters" described in the ARM (among others).
- --
- +-----------------------------------------------------------------------------+
- | Marc Girod - Nokia Telecommunications Phone: +358-0-511 7703 |
- | TL4E - P.O. Box 12 Fax: +358-0-511 7432 |
- | SF-02611 Espoo 61 - Finland Internet: marc.girod@ntc.nokia.com |
- | X.400: C=FI, A=Elisa, P=Nokia Telecom, UNIT=TRS, SUR=Girod, GIV=Marc |
- +-----------------------------------------------------------------------------+
-